zip: 匹配
>>> k = ['name','year','where'] >>> massage = ['tang','18','nc'] >>> other = [1,2,3] >>> list(zip(k,massage)) [('name', 'tang'), ('year', '18'), ('where', 'nc')] >>> list(zip(k,massage,other)) [('name', 'tang', 1), ('year', '18', 2), ('where', 'nc', 3)]
lambda:
>>> a =lambda x: x +1 >>> a(2) 3
filter过滤:
>>> li = [1,2,3] >>> a =lambda x: x >2 >>> b = filter(a,li) >>> list(b) [3]
map: 加工
>>> a =lambda x: x +1 >>> li = [1,2,3] >>> c=map(a,li) >>> c <map object at 0x0000014A0F4A86D8> >>> list(c) [2, 3, 4] >>> li = [1,2,3] >>> a =lambda x: x >1 >>> c=map(a,li) >>> list(c) [False, True, True]
enumerate枚举:
>>> li=['a','b','c','d'] >>> list(enumerate(li)) [(0, 'a'), (1, 'b'), (2, 'c'), (3, 'd')] >>> list(enumerate(li,10)) [(10, 'a'), (11, 'b'), (12, 'c'), (13, 'd')] for k,v in enumerate(li): print(k,v) >>> 0 a 1 b 2 c 3 d
进制,ascaii:
bin(3) # '0b11' 二进制前缀 0b
oct(9) # '0o11'
hex(17) # '0x11'
ord('a')
ord('A')
chr(65)
chr(97)
sorted:
sorted(iterable, /, *, key=None, reverse=False)
1)对于一个列表排序
sorted([100, 98, 102, 1, 40])
>>>[1, 40, 98, 100, 102]
2)通过key参数/函数
比如一个长列表里面嵌套了很多字典元素,我们要按照每个元素的长度大小排序
L = [{1:5,3:4},{1:3,6:3},{1:1,2:4,5:6},{1:9}] new_line=sorted(L,key=lambda x:len(x)) print(new_line) >>>[{1: 9}, {1: 5, 3: 4}, {1: 3, 6: 3}, {1: 1, 2: 4, 5: 6}]
3)对由tuple组成的List排序
比如下面是学生里面的年龄的一个list
students = [('wang', 'A', 15), ('li', 'B', 12), ('zhang', 'B', 10)] print(sorted(students, key=lambda x : x[2])) >>>[('zhang', 'B', 10), ('li', 'B', 12), ('wang', 'A', 15)]