Python学习笔记之列表

列表简介

列表(list) 是Python基本数据类型之一,是一种可变序列、可以存储各种数据类型的集合,用 [] 表示一个列表的开始和结束,元素之间使用 , 分隔,列表中的每个元素都有对应下标,下标从0开始。

列表基本操作

列表 支持对集合中元素进行增加、查找、删除、排序、合并等操作,常用方法下表所示:

方法功能
append在列表尾部增加元素
insert在列表指定位置插入元素
index返回指定元素的下标
remove从左到右遍历列表,删除第1个匹配的元素
clear清空列表
sort对列表中元素进行排序(列表中的元素需要相同类型)
count统计元素在当前列表中的个数
extend合并两个列表
reverse永久反转列表
copy浅拷贝列表
pop删除并返回指定下标对应的元素

在Python IDLE中可以通过help函数查看对应说明。

增加&插入

>>> test_list = []           #声明空列表
>>> test_list.append(1)      #调用append方法追加元素1
>>> test_list
[1]
>>> test_list.insert(0, 2)   #调用insert方法在0下标位置插入元素2
>>> test_list
[2, 1]

删除&清空

>>> test_list = [2, 1]
>>> test_list.pop(0)        #删除0位置元素
2
>>> test_list
[1]

>>> test_list = [1, 2, 3]
>>> test_list.remove(3)    #删除列表中元素3
>>> test_list
[1, 2]
>>> test_list = [3, 1, 2, 3]
>>> test_list.remove(3)    #可以看出删除的是左边第1个3元素
>>> test_list
[1, 2, 3]
>>> test_list.remove(5)    #删除一个不存在的元素时,抛出ValueError
Traceback (most recent call last):
  File "<pyshell#18>", line 1, in <module>
    test_list.remove(5)
ValueError: list.remove(x): x not in list

>>> test_list = [1, 2, 3]
>>> test_list.clear()       #清空列表
>>> test_list
[]

复制

>>> test_list = [1, 2, 3]            #声明test_list列表为[1,2,3]
>>> test_list2 = test_list.copy()    #copy
>>> test_list2
[1, 2, 3]
>>> id(test_list)
45812360
>>> id(test_list2)
46149960

统计指定元素个数

>>> test_list = [1, 2, 3, 1]
>>> test_list.count(1)            #统计test_list列表中元素为1的个数
2
>>> test_list.count(4)
0

查找

>>> test_list = [1, 2, 3, 4]
>>> test_list.index(1)            #在test_list查找元素1的下标 
0
>>> test_list.index(5)            #如果列表中并没有这个元素,抛出ValueError
Traceback (most recent call last):
  File "<pyshell#19>", line 1, in <module>
    test_list.index(5)
ValueError: 5 is not in list

排序&反转

>>> test_list = [1, 3, 2, 4]
>>> test_list.sort()               #排序,默认正序
>>> test_list
[1, 2, 3, 4]
>>> test_list.sort(reverse=True)   #倒序
>>> test_list
[4, 3, 2, 1]

>>> test_list = [1, 3, 2, 'Python']
>>> test_list.sort()               #列表中元素不同类型会抛出TypeError
Traceback (most recent call last):
  File "<pyshell#8>", line 1, in <module>
    test_list.sort()
TypeError: '<' not supported between instances of 'str' and 'int'

>>> test_list = ['Python', 'Java', 'Android', 'iOS']
>>> test_list.reverse()            #列表反转 
>>> test_list
['iOS', 'Android', 'Java', 'Python']

合并两个列表

>>> test_list1 = [1, 2, 3]
>>> test_list2 = [4, 5, 6]
>>> test_list1.extend(test_list2)  #将test_list2的元素合并到test_list1中
>>> test_list1
[1, 2, 3, 4, 5, 6]
>>> test_list2
[4, 5, 6]

查看指定下标元素&遍历

>>> test_list = ['Python', 'Java', 'Android', 'iOS']
>>> test_list[0]       #查看下标为0的元素
'Python'
>>> test_list[1]       #查看下标为1的元素
'Java'
>>> test_list[2]       #查看下标为2的元素
'Android'
>>> test_list[3]       #查看下标为3的元素
'iOS'
>>> test_list[4]       #查看下标为4的元素,这里没有下标为4,抛出IndexError
Traceback (most recent call last):
  File "<pyshell#14>", line 1, in <module>
    test_list[4]
IndexError: list index out of range
------------------------------------------------------------
>>> test_list = ['Python', 'Java', 'Android', 'iOS']
>>> test_list[:]                #切片
['Python', 'Java', 'Android', 'iOS']
>>> for item in test_list:      #for循环
	print(item)	
Python
Java
Android
iOS

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值