列表 list

  列表其实就是JavaScript中的数组。  

  列表是常用的数据类型之一。通过列表可以对数据实现最方便的存储、修改等操作

【1】定义列表

aList = [a,b,c,d,e]

【2】访问列表中的元素

>>> aList = ['a','b','c','d','e']
>>> aList[0]
'a'
>>> aList[3]
'd'
>>> aList[-1]
'e'
>>> aList[-3]
'c'
>>>

【3】列表的更新

>>> aList = ['a','b','c','d','e']
>>> aList[1] = 'B'
>>> aList
['a', 'B', 'c', 'd', 'e']

【4】列表操作符

  

Python 表达式结果描述
len([1, 2, 3]) 3 长度
[1, 2, 3] + [4, 5, 6] [1, 2, 3, 4, 5, 6] 组合
['Hi!'] * 4 ['Hi!', 'Hi!', 'Hi!', 'Hi!'] 重复
3 in [1, 2, 3] True 元素是否存在于列表中
for x in [1, 2, 3]: print(x, end=" ") 1 2 3 迭代
  • len() 获取类标的长度
>>> aList = ['a','b','c','d','e']
>>> len(aList)
5
  • +  组合列表
>>> aList = ['a','b','c','d','e']
>>> aList1 = ['f','g','h','i']
>>> aList + aList1
['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i']
>>> aList
['a', 'b', 'c', 'd', 'e']
>>> aList1
['f', 'g', 'h', 'i']
  • * 重复列表
>>> aList = ['a','b','c','d','e']
>>> aList * 2
['a', 'b', 'c', 'd', 'e', 'a', 'b', 'c', 'd', 'e']
>>> aList
['a', 'b', 'c', 'd', 'e']
  • in 判断元素在不在列表中,not in 判断元素是否不在列表中
>>> ['a', 'b', 'c', 'd', 'e']
['a', 'b', 'c', 'd', 'e']
>>> aList = ['a','b','c','d','e']
>>> 'b' in aList
True
>>> 'g' in aList
False
>>> 'b' not in aList
False
>>> 'h' not in aList
True
  • for...in 迭代列表(循环列表)
aList = ['a','b','c','d','e']
for x in aList:
    print(x)
#a
#b
#c
#d
#e

【5】列表的常见操作()

  • 切片:取多个元素
>>> aList = ['a','b','c','d','e']
>>> aList[1:4]      #取下标1至下标4之间的数字,包括1,不包括4  
['b', 'c', 'd']
>>> aList[1:-1]    #取下标1至-1的值,不包括-1
['b', 'c', 'd']
>>> aList[0:3]    #取下标0至3的值,不包括3
['a', 'b', 'c']
>>> aList[:3]      #如果是从头开始取,0可以忽略,跟上句效果一样
['a', 'b', 'c']
>>> aList[3:]      #如果想取最后一个,必须不能写-1,只能这么写
['d', 'e']
>>> aList[3:-1]    #如果想取最后一个,必须不能写-1,只能这么写
['d']
>>> aList[0::2]    #后面的2是代表,每隔一个元素,就取一个
['a', 'c', 'e']
>>> aList[::2]     #后面的2是代表,每隔一个元素,就取一个
['a', 'c', 'e']
>>> aList[::-2]     #从开始取到结束,步长-2,每隔一个,取一个元素,从最后开始算
['e', 'c', 'a']    
  • len() 对字符串来说 len()返回字符串的长度,就是字符串包含的字符个数.对列表或者元组来说, 它会像你想像的那样返回列表或者元组的元素个数 
>>> aList = ['a','b','c','d','e']
>>> len(aList)
5
  • max() and min()  对于字符串则返回字典序的大小,对于数字则返回数字的最大或者最小
>>> aList = ['a','b','c','d','e']
>>> max(aList)
'e'
>>> min(aList)
'a'
>>> bList =[2,6,4,8,4,5]
>>> max(bList)
8
>>> min(bList)
2
  • reverse()反向列表中元素
>>> aList = ['a','b','c','d','e']
>>> aList.reverse()
>>> aList
['e', 'd', 'c', 'b', 'a']
  •  sort(func)  对原列表进行排序
>>> names=['Alex', 'Tenglan', 'Amy', 'Tom', 'Amy', 1, 2, 3]
>>> names.sort() #排序
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: unorderable types: int() < str()   #3.0里不同数据类型不能放在一起排序了,擦
>>> names[-3] = '1'
>>> names[-2] = '2'
>>> names[-1] = '3'
>>> names
['Alex', 'Amy', 'Amy', 'Tenglan', 'Tom', '1', '2', '3']
>>> names.sort()
>>> names
['1', '2', '3', 'Alex', 'Amy', 'Amy', 'Tenglan', 'Tom']

>>> aList = [1,2,3,11,22,33]
>>> aList.sort()
>>> aList
  [1, 2, 3, 11, 22, 33]

  • append(obj)   在列表末尾添加新的对象 
>>> aList = ['a','b','c']
>>> aList.append('e')
>>> aList
['a', 'b', 'c', 'e']
  • count()统计某个元素在列表中出现的次数
>>> aList = ['a','b','c','a','a']
>>> aList.count('a')
3
  • index() 从列表中找出某个值第一个匹配项的索引位置
>>> aList = ['a','b','c','b','a']
>>> aList.index('b')
1
  • insert(index,obj)将对象插入列表 
>>> aList = ['a,','b','c','d','e']
>>> aList.insert(1,'g')
>>> aList
['a,', 'g', 'b', 'c', 'd', 'e']
>>> aList.insert(4,'g')
>>> aList
['a,', 'g', 'b', 'c', 'g', 'd', 'e']
  • pop()移除列表中的一个元素(默认是最后一个元素),并且返回该元素的值
>>> aList = ['a,','b','c','d','e']
>>> aList.pop()
'e'
  •  remove(obj) 移除列表中指定元素
>>> aList = ['a,','b','c','d','e']
>>> aList.remove('c')
>>> aList
['a,', 'b', 'd', 'e']
  • del 删除列表中的指定下标元素
>>> aList = ['a,','b','c','d','e']
>>> del aList[2]
>>> aList
['a,', 'b', 'd', 'e']
  • clear() 清空列表
>>> aList = ['a,','b','c','d','e']
>>> aList.clear()
>>> aList
[]
  • extend() 通过extend可以将另一个集合中的元素逐一添加到列表中
>>> a = [1, 2]
>>> b = [3, 4]
>>> a.append(b)
>>> a
[1, 2, [3, 4]]
>>> a.extend(b)
>>> a
[1, 2, [3, 4], 3, 4]
  •  copy() 复制列表
>>> aList = ['a','b','c','d']
>>> bList = aList.copy()
>>> bList
['a', 'b', 'c', 'd']

  关于列表的复制的深入理解,点击此

【6】列表的嵌套

  一个列表中的元素又是一个列表,这就是列表的嵌套

aList = ['a','b',['a','b','c','d'],'c','d']

 补充 enumerate()

aList = ['a','b','c','d']
for n, val in enumerate(aList):
    print("%d %s"%( n+1,val))
#1 a
#2 b
#3 c
#4 d

原文地址:https://www.cnblogs.com/Jiangchuanwei/p/8484825.html