1、解压序列(列表、元组、字符串)赋值给多个变量:
>>>p=('a',5,(1,2))
>>>x,y,z=p
>>>x
'a'
>>>y
5
>>>z
(1,2)
>>>s=‘Hello'
>>>a,b,c,d,e=s
>>>a
'H'
>>>b
e
......
2、用*表达式解决序列元素个数超过变量个数:
>>>a=('a', 25, ..., '000-001')
>>>first,*mid,last=a
>>>first
'a'
>>>*mid
[25, ...,]
>>>last
'000-001'
>>>a=[1,2,3,4]
>>>*_ ,last=a
>>>last
4
>>>line='a:b:c:d:e'
>>>l1,*_,l4,l5=line.split(':')
>>>l1
a
>>>l4
d
>>>l5
e
3、用上述分割方法实现 递归算法:
>>>def sum(items):
h,*t=items
return h+sum(t) if t else h
>>>sum([2,4,5,1,3])
15
4、用collections.defaultdict实现字典中一个键对应多个值:
>>>from collections import defaultdict
>>>d=defaultdict(list)
>>>d['a'].append(1)
>>>d['a'].append(2)
>>>d['b'].append(3)
>>>d
{'a':[1,2],'b':[3]}
>>>d=defaultdict(set)
>>>d['a'].add(1)
>>>d[;a'].add(2)
>>>d['b'].add(3)
>>>d
{'a':{1,2},'b':{3}}
5、在字典中执行求最小值、最大值、排序:
>>>p={ 'a': 45, 'b': 612, 'c':12, 'd': 234}
>>>minp=min(zip(p.values(),p.keys()))
>>>minp
(12,'c')
>>>maxp=max(zip(p.values(),p.keys()))
>>>maxp
(612,'b')
>>>sortp=sorted(zip(p.values(),p.keys()))
>>>sortp
[(12,'c'),(45,'a'),(234,'d')(612,'b')]
6、在两个字典中寻找相同键、不同键、相同值:
>>>a={'x':1,'y':2,'z':3}
>>>b={'w':9,'x':11,'y':2}
>>>a.keys()&b.keys()
{'x','y'}
>>>a.keys()-b.keys()
{'z'}
>>>a.items()&b.items()
{('y',2)}
7、消除列表中的重复元素:
>>>a=[1,5,2,1,9,1,5,10]
>>>list(set(a))
[1,2,10,5,9]
8、列表元素去重、排序:
>>>a=[1,5,2,1,9,1,5,10]
>>>list(set(a)).sort()
[1,2,5,9,10]
9、切片操作:
>>>items=[0,1,2,3,4,5,6]
>>>a=slice(2,4)
>>>a.start
2
>>>a.stop
4
>>>a.step
1
>>>items[a]
[2,3]
>>>items[a]=[10,11]
>>>items
[0,1,10,11,4,5,6]
>>>del items[a]
>>>items
[0,1,4,5,6]
10、找出序列中出现次数最多的元素:
>>>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']
>>>from collections import Counter
>>>word_counts = Counter(words)
>>># 出现频率最高的 3 个单词
>>>top_three = word_counts.most_common(3)
>>>print(top_three)
[('eyes', 8), ('the', 5), ('look', 4)]
11、统计序列中某元素个数:
>>>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']
>>>from collections import Counter
>>>word_counts['eyes']
8
12、通过某个关键字排序一个字典列表:
>>>rows = [
{'a': 'B', 'b': 'J', 'uid': 1003},
{'a': 'D', 'b': 'B', 'uid': 1002},
{'a': 'J', 'b': 'C', 'uid': 1001},
{'a': 'B', 'b': 'J', 'uid': 1004}
]
>>>from operator import itemgetter
>>>rows_by_a = sorted(rows, key=itemgetter('a'))
>>>print(rows_by_a)
[{'a': 'B', 'uid': 1004, 'b': 'J'},
{'a': 'B', 'uid': 1003, 'b': 'J'},
{'a': 'D', 'uid': 1002, 'b': 'B'},
{'a': 'J', 'uid': 1001, 'b': 'C'}]
13、列表推导式:
>>> mylist = [1, 4, -5, 10, -7, 2, 3, -1]
>>> [n for n in mylist if n > 0]
[1, 4, 10, 2, 3]
14、字典推导式:
>>>prices = {
'ACME': 45.23,
'AAPL': 612.78,
'IBM': 205.55,
'HPQ': 37.20,
'FB': 10.75
}
>>>p1 = dict((key, value) for key, value in prices.items() if value > 200)
>>>p1
......
15、转换并同时计算数据:
>>>nums = [1, 2, 3, 4, 5]
>>>s = sum(x * x for x in nums) #求平方和
>>>minv=min(nums) #求最小值
>>>maxv=max(nums) #求最大值
16、合并多个字典或映射:
>>>a = {'x': 1, 'z': 3 }
>>>b = {'y': 2, 'z': 4 }
>>>from collections import ChainMap
>>>c = ChainMap(a,b) #新的字典或映射