列表是一系列元素,按特定顺序排列组成。列表总的元素之间没有任何关系,既可以时字符串,也可以是数字,还可以是布尔值。
由此可以看出,列表通常包含多个元素,因此再给列表命名的时候,最好使用复数,例如:names、ages等
>>> a = [1,2,3] >>> print(a,type(a)) [1, 2, 3] <class 'list'>
>>> b = list('abc') >>> b,type(b) ['a', 'b', 'c'] <class 'list'> >>> c = list((1,2,3)) >>> c [1, 2, 3] # 对于字典,生成的是键 key 列表。 >>> cc = list({"aa":1,"bb":3}) >>> cc ['aa', 'bb']
>>> d = list(range(10)) >>> print(d, type(d)) [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] <class 'list'> >>> dd = list(range(3,-10,-3)) >>> print(dd, type(dd)) [3, 0, -3, -6, -9] <class 'list'>
>>> f = [i for i in range(5)] >>> print(f,type(f)) [0, 1, 2, 3, 4] <class 'list'>
>>> g = list() >>> print(g) [] >>> h = [] >>> print(h) []
注:我们发现在尾部添加元素后, b 的存储地址没有改变。原地操作列表。
>>> a = ['QQ','微信'] >>> b = [] >>> print(id(b)) 51115016 >>> for i in a: b.append(i) >>> print(b) ['QQ', '微信'] >>> print(id(b)) 51115016
注:将一个列表的元素,添加到另外一个列表元素的尾部。添加元素前后,地址不变。
>>> c = [1,2,3] >>> print(id(c)) 51296456 >>> d = ['QQ', '微信'] >>> c.extend(d) >>>> print(c,id(c)) [1, 2, 3, 'QQ', '微信'] 51296456
注:在列表指定位置插入元素。当涉及大量元素时,尽量避免使用。因为,会让插入位置后面的元素进行大面积移动,影响处理速度。类似这样的函数还有:remove、 pop、del。
>>> c = [1,2,3] >>> c.insert(2,'qq') # insert(索引, 要插入的元素) >>> c [1, 2, 'qq', 3]
注:因为存储地址前后发生改变,属于创建了新列表,增加了内存。不建议使用。
>>> a = [20,40] >>> print(id(a)) 51297096 >>> a = a + [50] >>> print(a) [20, 40, 50] >>> print(id(a)) 51296968
注:不建议使用。(字符串、元组也可以用‘ 乘法扩展’ ,增加相同的元素)
>>> a = [1,2,3] >>> print(id(a)) 51297416 >>> b = a * 3 >>> print(b) [1, 2, 3, 1, 2, 3, 1, 2, 3] >>> print(id(b)) 51225352
注:若列表中,有重复元素,则删除列表中,首次出现的该元素。若删除元素不存在,则抛出异常。
>>> d = [1,2,5,2,3,4,5,1,3,2,1] >>> d.remove(2) >>> d [1, 5, 2, 3, 4, 5, 1, 3, 2, 1]
注:删除列表指定位置的元素。
>>> a = [1,2,3] >>> del a[0] >>> print(a) [2, 3] #删除整个列表 >>> del a >>> a Traceback (most recent call last): File "<pyshell#417>", line 1, in <module> a NameError: name 'a' is not defined
注:删除指定位置元素,并返回指定位置元素。若不指定位置,默认删除列表末 尾元素。推荐使用:不指定索引的用法。
>>> b = [1,2,3] >>> b.pop(1) 2 >>> print(b) [1, 3] >>> c = ['a','b','c'] >>> c.pop() 'c' >>> c.pop() 'b' >>> c.pop() 'a' >>> print(c) [] >>> c.pop() Traceback (most recent call last): File "<pyshell#13>", line 1, in <module> c.pop() IndexError: pop from empty list
>>> d = [1,2,5,2,3,4,5,1,3,2,1] >>> d.clear() >>> d []
关于这一小节,我们讲述2种获取列表元素的方法,1种获取列表元素位置的方法。
注意:如果索引长度,超过列表长度,则会报错。记住下面这个错误,以后会经常遇到。
>> a = [1,2,3,4,5,6,7,8,9] >>> a[4] 5 > >> a[9] Traceback (most recent call last):31 File "<pyshell#28>", line 1, in <module> a[9] IndexError: list index out of range
>>> a = [1,2,3,4,5,6,7,8,9] >>> a[3:] [4, 5, 6, 7, 8, 9] >>> a[2:6] [3, 4, 5, 6] >>> a[::-1] [9, 8, 7, 6, 5, 4, 3, 2, 1]
语法:index(指定元素,start,end), start、 end 表示从哪个位置开始进行搜索,如果待搜索的元素不在搜索范围内,会报错。注意:传入列表元素,返回该元素的索引。
>>> b = [12,13,14,12,12,14,13,12,14,15] >>> b.index(13) 1 >>> b.index(13,2) 6 >>> b.index(13,2,5) Traceback (most recent call last): File "<stdin>", line 1, in <module> ValueError: 13 is not in list
有时候我们需要统计某个列表的长度(列表中有几个元素),或者有时候需要对列表中的某个元素进行计数(数量统计)。
>>> b = [12,13,14,12,12,14,13,12,14,15] >>> len(b) 10
注意:获取指定元素在列表中出现的次数
>>> a = [12,13,14,12,12,14,13,12,14,15] >>> a.count(14) 3