Python3学习笔记 | 六、Python的类型与运算-列表
- 2019 年 10 月 6 日
- 筆記
一、列表简介
列表在Python里是有序集合对象类型,列表里的对象可以是任何对象:数字,字符串,列表或者之后会讲到的字典、元组等等。 与字符串不同,列表是可变对象,支持原处修改的操作。Python中的列表可以完成大多数集合体数据结构的工作,而这些在稍底层一些的语言中你不得不手工去实现。
Python的列表是: • 任意对象的有序集合 • 通过偏移读取 • 可变长度、异构以及任意嵌套 • 属于可变序列的分类 • 对象引用数组
二、列表的操作
1、与字符串相同的操作
之前在字符串里的大部分操作都可以用在列表,比如 合并与重复: List1 + List2 : 结果是两个列表按顺序结合。 List1 * 3 : 结果是列表1重复三次。 for i in List1: print i : 按顺序打印列表里的内容 3 in List : 判断列表里有没有一个对象是对象3 List1.index(1) : 查找列表里第一个为1的对象的位置 List1.count(1): 查找列表里对象为1的个数 List1[x:y]: 取第x到y的对象,重新建立一个列表 len(List1): List1里的对象个数
2、基本列表操作
创建一个列表:
>>> list1=[1,2,3,4]
对列表内容取值:
>>> list1=[1,2,3,4,5,6,7] >>> list1[6] 7 >>> list1[3:6] [4, 5, 6]
重复:
>>> list1*3 [1, 2, 3, 4, 5, 6, 7, 1, 2, 3, 4, 5, 6, 7, 1, 2, 3, 4, 5, 6, 7]
3、列表迭代和解析
使用in方法来判断:
>>> 7 in list1 True
循环打印列表内容:
>>> for i in list1: ... print(i,end=' ') ... 1 2 3 4 5 6 7
迭代方式创建列表:
>>> list2=[D*3 for D in 'Dora'] >>> list2 ['DDD', 'ooo', 'rrr', 'aaa'] >>> list(map(abs,[-3,-2,-1,0,1,2,3])) [3, 2, 1, 0, 1, 2, 3]
其中map()函数用来返回一个列表,abs则是之前提到了取绝对值
4、索引、分片与矩阵
这个方法和字符串里的方法相似:
>>> list3=['Dora','Emon','DaXiong'] >>> list3[1] 'Emon' >>> list3[0:2] ['Dora', 'Emon'] >>> list3[-3] 'Dora'
矩阵方式:
>>> Dora=[[1,2,3,4],[5,6,7,8],[9,10,11,12]] >>> Dora[2] [9, 10, 11, 12] >>> Dora[2][1]10
5、原处修改
跟字符串不同,列表可以在原处修改:
>>> D=['Dora','Emon'] >>> D ['Dora', 'Emon'] >>> D[2:]=['Da','Xiong'] >>> D ['Dora', 'Emon', 'Da', 'Xiong']
添加元素与排序:
>>> D.append('Jing') >>> D ['Dora', 'Emon', 'Da', 'Xiong', 'Jing'] >>> D.sort() >>> D ['Da', 'Dora', 'Emon', 'Jing', 'Xiong']
三、列表的方法
1、sort、sorted
python中列表的内置函数sort()可以对列表中的元素进行排序,而全局性的sorted()函数则对所有可迭代的序列都是适用的;并且sort()函数是内置函数,会改变当前对象,而sorted()函数只会返回一个排序后的当前对象的副本,而不会改变当前对象。
>>> list4=['Dora','Emon','Da','Xiong'] >>> list4.sort(key=str.lower) >>> list4 ['Da', 'Dora', 'Emon', 'Xiong'] >>> list4.sort(key=str.lower,reverse=True) >>> list4 ['Xiong', 'Emon', 'Dora', 'Da'] >>> >>> list4=['Dora','Emon','Da','Xiong'] >>> sorted(list4) ['Da', 'Dora', 'Emon', 'Xiong'] >>> sorted(list4,key=str.lower,reverse=True) ['Xiong', 'Emon', 'Dora', 'Da'] >>> sorted([x.lower() for x in list4],reverse=True) ['xiong', 'emon', 'dora', 'da']
其中key用来指定排列规则,reverse用来表示是否倒序。
2、extend、pop、reverse
extend()函数用来扩展列表元素,当然列表也支持相加 pop()函数可以默认移除列表最后一个元素的效果,也可对指定位置进行移除 reverse()函数则使列表倒序
>>> list5=['Dora','Emon','Da','Xiong'] >>> list5.extend(['Jing','Xiang']) >>> list5 ['Dora', 'Emon', 'Da', 'Xiong', 'Jing', 'Xiang'] >>> list5.pop()'Xiang' >>> list5 ['Dora', 'Emon', 'Da', 'Xiong', 'Jing'] >>> list5.pop(2)'Da' >>> list5 ['Dora', 'Emon', 'Xiong', 'Jing'] >>> list5.reverse() >>> list5 ['Jing', 'Xiong', 'Emon', 'Dora']
3、index、insert、del
index()函数用来返回元素位置 insert()函数用来对指定位置进行插入元素 sel则是删除指定位置元素
>>> list6=['Dora','Emon','Da','Xiong'] >>> list6.index('Da') 2 >>> list6.insert(4,'Jing') >>> list6 ['Dora', 'Emon', 'Da', 'Xiong', 'Jing'] >>> del list6[4] >>> list6 ['Dora', 'Emon', 'Da', 'Xiong'] >>> del list6[0:] >>> list6 []
四、实际用法
1、取值
当一个列表里的内容是有固定含义时,比如:[‘姓名’, 年龄,[出生年,月,日]],像是这样:[‘Dora’,77,[1942,6,4]]
>>> info=['Dora',77,[1942,6,4]] >>> name,age,brith=info >>> print(str(name)+str(age)+str(brith)) Dora77[1942, 6, 4] >>> name,age,(brith_y,brith_m,brith_d)=info >>> print(str(name)+str(age)+str(brith_y)+str(brith_m)+str(brith_d)) Dora77194264
当一个列表长度不固定,比如: [‘Abby’,’[email protected]’,’111-222-333’,’111-222-222’] [‘Bobby’,’[email protected]’,’111-333-333’] 这时候就可以使用*代表多个元素
>>> A_name, A_mail, *A_phone = ['Abby','[email protected]','111-222-333','111-222-222'] >>> B_name, B_mail, *B_phone = ['Bobby', '[email protected]', '111-333-333'] >>> print(A_name, A_mail, A_phone) Abby [email protected] ['111-222-333', '111-222-222'] >>> print(B_name, B_mail, B_phone) Bobby [email protected] ['111-333-333']
只保留列表里最后N个记录: 使用deque模块限定数组最大长度,并且有新元素添加的时候,数组整体将左移或右移。
>>> from collections import deque >>> q = deque(maxlen=3) >>> q.append('a') >>> q.append('b') >>> q.append('c') >>> q deque(['a', 'b', 'c'], maxlen=3) >>> q.append('d') >>> q deque(['b', 'c', 'd'], maxlen=3) >>> q.appendleft('e') >>> q deque(['e', 'b', 'c'], maxlen=3) >>> q.pop()'c' >>> q deque(['e', 'b'], maxlen=3)
2、查找最大,最小值
查找最大值最小值可以直接使用max与min,但是想要获取几个最大或者最小的数就需要用到nlargest与nsmallest了。
>>> nums = [1, 8, 2, 23, 7, -4, 18, 23, 42, 37, 2] >>> max(nums) 42 >>> min(nums) -4 >>> sum(nums) 159 >>> >>> from heapq import nlargest, nsmallest >>> nlargest(3,nums) [42, 37, 23] >>> nsmallest(3,nums) [-4, 1, 2]
3、命名切片
之前切片都是使用nums[0:2]的方式,其实也可以使用slice函数来具体定义切片的名称,但是以下操作在python3中并没有实现,python2可以正常使用。
>>> nums = [0,1,2,3,4,5,6] >>> _slice1 = slice(0,2) >>> _slice2 = slice(2,4) >>> _slice3 = slice(3,7) >>> nums[_slice1] [0, 1] >>> nums[_slice2] [2, 3] >>> nums[_slice3] [3, 4, 5, 6]
4、重复的元素计算
collections里的Counter模块可以计算列表里重复元素的个数:
>>> from collections import Counter >>> words = ['look', 'into', 'my', 'eyes', 'look', 'into', 'my', 'eyes', ... 'the', 'eyes', 'the', 'eyes', 'the', 'eyes', 'not', 'around', 'the', ... 'eyes', "don't", 'look', 'around', 'the', 'eyes', 'look', 'into', ... 'my', 'eyes', "you're", 'under'] >>> countered_words = Counter(words) >>> countered_words Counter({'eyes': 8, 'the': 5, 'look': 4, 'into': 3, 'my': 3, 'around': 2, "you're": 1, "don't": 1, 'under': 1, 'not': 1}) >>> countered_words.most_common(2) [('eyes', 8), ('the', 5)] >>> morewords = ['why','are','you','not','looking','in','my','eyes'] >>> countered_words.update(morewords) >>> countered_words Counter({'eyes': 9, 'the': 5, 'look': 4, 'my': 4, 'into': 3, 'not': 2, 'around': 2, "you're": 1, "don't": 1, 'in': 1, 'you': 1, 'looking': 1, 'are': 1, 'under': 1, 'why': 1})