deque from collections import deque q = deque(maxlen=3) q.append(1) q.append(2) q.append(3) q.append(4) q

deque([2, 3, 4], maxlen=3) q.pop() 4 q.popleft() 2 解构实现递归 def all_sum(numbers): head,*total = numbers return head+all_sum(total) if total else head items = [1, 10, 7, 4, 5, 9] all_sum(items) 36 寻找最大最小的n个元素 import heapq nums = [1, 8, 2, 23, 7, -4, 18, 23, 42, 37, 2]

print(heapq.nlargest(3, nums))

print(heapq.nsmallest(3, nums))

portfolio = [ {'name': 'IBM', 'shares': 100, 'price': 91.1}, {'name': 'AAPL', 'shares': 50, 'price': 543.22}, {'name': 'FB', 'shares': 200, 'price': 21.09}, {'name': 'HPQ', 'shares': 35, 'price': 31.75}, {'name': 'YHOO', 'shares': 45, 'price': 16.35}, {'name': 'ACME', 'shares': 75, 'price': 115.65} ] cheap = heapq.nsmallest(3, portfolio, key=lambda s: s['price']) expensive = heapq.nlargest(3, portfolio, key=lambda s: s['price']) expensive

heapq.heapify(nums) # [-4, 2, 1, 23, 7, 2, 18, 23, 42, 37, 8]

heapq.heappop(nums) # -4

字典中的键对应多个值 from collections import defaultdict

list_container = defaultdict(list) list_container['a'].append(1) list_container['a'].append(2) list_container['b'].append(4) set_container = defaultdict(set) set_container['a'].add(1) set_container['a'].add(2) set_container['b'].add(4) 按照字典的插入值的顺序保留顺序 from collections import OrderedDict

d = OrderedDict() d['foo'] = 1 d['bar'] = 2 d['spam'] = 3 d['grok'] = 4

Outputs "foo 1", "bar 2", "spam 3", "grok 4"

for key in d: print(key, d[key]) 字典的运算 prices = { 'ACME': 45.23, 'AAPL': 612.78, 'IBM': 205.55, 'HPQ': 37.20, 'FB': 10.75 }

max(prices.keys()) # 'IBM' max(prices.values()) # 612.78

上面两种方式只能得到最大最小的值,并不能知道这个值或键对应的键或值

max(zip(prices.values(),prices.keys())) sorted(zip(prices.values(),prices.keys()))

[(10.75, 'FB'), (37.2, 'HPQ'), (45.23, 'ACME'), (205.55, 'IBM'), (612.78, 'AAPL')] 查找两个字典中相同的键或值 a = { 'x' : 1, 'y' : 2, 'z' : 3 }

b = { 'w' : 10, 'x' : 11, 'y' : 2 } a.items() & b.items() #{('y', 2)}

a.keys() - b.keys()

{('y', 2)} 序列中元素的出现次数 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)

Outputs [('eyes', 8), ('the', 5), ('look', 4)]

通过某个关键字给字典列表排序 from operator import itemgetter rows = [ {'fname': 'Brian', 'lname': 'Jones', 'uid': 1003}, {'fname': 'David', 'lname': 'Beazley', 'uid': 1002}, {'fname': 'John', 'lname': 'Cleese', 'uid': 1001}, {'fname': 'Big', 'lname': 'Jones', 'uid': 1004} ]

rows_by_fname = sorted(rows, key=itemgetter('fname')) rows_by_uid = sorted(rows, key=itemgetter('uid')) row_by_lambda = sorted(rows,key=lambda x:x['fname']) row_by_lambda

[{'fname': 'Big', 'lname': 'Jones', 'uid': 1004}, {'fname': 'Brian', 'lname': 'Jones', 'uid': 1003}, {'fname': 'David', 'lname': 'Beazley', 'uid': 1002}, {'fname': 'John', 'lname': 'Cleese', 'uid': 1001}] 通过某个字段将列表中的元素分组 rows = [ {'address': '5412 N CLARK', 'date': '07/01/2012'}, {'address': '5148 N CLARK', 'date': '07/04/2012'}, {'address': '5800 E 58TH', 'date': '07/02/2012'}, {'address': '2122 N CLARK', 'date': '07/03/2012'}, {'address': '5645 N RAVENSWOOD', 'date': '07/02/2012'}, {'address': '1060 W ADDISON', 'date': '07/02/2012'}, {'address': '4801 N BROADWAY', 'date': '07/01/2012'}, {'address': '1039 W GRANVILLE', 'date': '07/04/2012'}, ]

from operator import itemgetter from itertools import groupby

Sort by the desired field first

rows.sort(key=itemgetter('date'))

Iterate in groups

for date, items in groupby(rows, key=itemgetter('date')): print(date) for i in items: print(' ', i)