第一章 資料結構與演算法
1.1 將序列分解為單獨的變數
p = (4, 5) x, y = p print x print y data = [ 'ACME', 50, 91.1, (2012, 12, 21) ] name, shares, price, date = data print name print shares print price print date name, shares, price, (year, mon, day ) = data print year p = (4, 5) #x, y, z = p 错误!!! s = 'hello!' a, b, c, d, e, f = s print a print f data = [ 'ACME', 50, 91.1, (2012, 12, 21) ] _, shares, price, _ = data print shares print price #其他数据可以丢弃了
1.2 從任何長度 找到最大或最小的N個元素
from audioop import avg def drop_first_last(grades): first, *middle, last = grades return avg(middle) record = ('Dave', 'dave@example.com', '777-333-2323', '234-234-2345') name, email, *phone_numbers = record print name print email print phone_numbers *trailing, current = [10, 8, 7, 2, 5] print trailing #[10, 8, 7, 2, ] print current #5 records = [ ('foo', 1, 2), ('bar', 'hello'), ('foo', 5, 3) ] def do_foo(x, y): print ('foo', x, y) def do_bar(s): print ('bar', s) for tag, *args in records: if tag == 'foo': do_foo(*args) elif tag == 'bar': do_bar(*args) line = 'asdf:fedfr234://wef:678d:asdf' uname, *fields, homedir, sh = line.split(':') print uname print homedir record = ('ACME', 50, 123.45, (12, 18, 2012)) name, *_, (*_, year) = record print name print year items = [1, 10, 7, 4, 5, 9] head, *tail = items print head #1 print tail #[10, 7, 4, 5, 9] def sum(items): head, *tail = items return head + sum(tail) if tail else head sum(items)
1.5 實現優先權隊列
from _collections import deque def search(lines, pattern, history=5): previous_lines = deque(maxlen = history) for line in lines: if pattern in line: yield line, previous_lines previous_lines.append(line) # Example use on a file if __name__ == '__main__': with open('somefile.txt') as f: for line, prevlines in search(f, 'python', 5): for pline in prevlines: print (pline) #print (pline, end='') print (line) #print (pline, end='') print ('-'*20) q = deque(maxlen=3) q.append(1) q.append(2) q.append(3) print q q.append(4) print q q = deque() q.append(1) q.append(2) q.append(3) print q q.appendleft(4) print q q_pop = q.pop() print q_pop print q q_popleft = q.popleft() print q_popleft print q
1.6 在字典中將構建映射到多個值上
import heapq nums = [1,30,6,2,36,33,46,3,23,43] print (heapq.nlargest(3, nums)) print (heapq.nsmallest(3, nums)) portfolio = [ {'name':'IBM', 'shares':100, 'price':2.4}, {'name':'A', 'shares':1040, 'price':12.4}, {'name':'S', 'shares':40, 'price':23.4}, {'name':'D', 'shares':1, 'price':2.49}, {'name':'F', 'shares':9, 'price':24} ] cheap = heapq.nsmallest(3, portfolio, key=lambda s: s['price']) expensive = heapq.nlargest(3, portfolio, key=lambda s: s['price']) print cheap print expensive nums = [1,8,2,23,7,-4,18,23,42,37,2] heap = list(nums) print heap heapq.heapify(heap) print heap print heapq.heappop(heap) print heapq.heappop(heap) print heapq.heappop(heap)
1.7 在字典中將構建映射到多個值上
import heapq class PriorityQueue: def __init__(self): self._queue = [] self._index = 0 def push(self, item, priority): heapq.heappush(self._queue, (-priority, self._index, item)) self._index += 1 def pop(self): return heapq.heappop(self._queue)[-1] #Example class Item: def __init__(self, name): self.name = name def __repr__(self): return 'Item({!r})'.format(self.name) q = PriorityQueue() q.push(Item('foo'), 1) q.push(Item('spam'), 4) q.push(Item('bar'), 5) q.push(Item('grok'), 1) print q.pop() print q.pop() print q.pop() a = Item('foo') b = Item('bar') #a < b error a = (1, Item('foo')) b = (5, Item('bar')) print a < b c = (1, Item('grok')) #a < c error a = (1, 0, Item('foo')) b = (5, 1, Item('bar')) c = (1, 2, Item('grok')) print a < b print a < c
1.7 在字典中保持有序? reee
1.9 在兩個字典中尋找相同點
d = { 'a' : [1, 2, 3], 'b' : [4, 5] } e = { 'a' : {1, 2, 3}, 'b' : {4, 5} } from collections import defaultdict d = defaultdict(list) d['a'].append(1) d['a'].append(2) d['a'].append(3) print d d = defaultdict(set) d['a'].add(1) d['a'].add(2) d['a'].add(3) print d d = {} d.setdefault('a', []).append(1) d.setdefault('a', []).append(2) d.setdefault('b', []).append(3) print d d = {} for key, value in d:#pairs: if key not in d: d[key] = [] d[key].append(value) d = defaultdict(list) for key, value in d:#pairs: d[key].append(value)1.10 從序列中移除重複項且保持元素間順序不變
from collections import OrderedDict d = OrderedDict() d['foo'] = 1 d['bar'] = 2 d['spam'] = 3 d['grol'] = 4 for key in d: print (key, d[key]) import json json.dumps(d)中出現次數最多的元素
price = { 'ACME':23.45, 'IBM':25.45, 'FB':13.45, 'IO':4.45, 'JAVA':45.45, 'AV':38.38, } min_price = min( zip( price.values(), price.keys() ) ) print min_price max_price = max( zip( price.values(), price.keys() ) ) print max_price price_sorted = sorted( zip( price.values(), price.keys() ) ) print price_sorted price_and_names = zip( price.values(), price.keys() ) print (min(price_and_names)) #print (max(price_and_names)) error zip()创建了迭代器,内容只能被消费一次 print min(price) print max(price) print min(price.values()) print max(price.values()) print min(price, key = lambda k : price[k]) print max(price, key = lambda k : price[k]) min_value = price[ min(price, key = lambda k : price[k]) ] print min_value price = { 'AAA': 23, 'ZZZ': 23, } print min( zip( price.values(), price.keys() ) ) print max( zip( price.values(), price.keys() ) )1.13 透過公鍵對字典清單排序
a = { 'x':1, 'y':2, 'z':3 } b = { 'x':11, 'y':2, 'w':10 } print a.keys() & b.keys() #{'x','y'} print a.keys() - b.keys() #{'z'} print a.items() & b.items() #{('y', 2)} c = {key: a[key] for key in a.keys() - {'z', 'w'} } print c #{'x':1, 'y':2}1.14 對不原生支援比較操作的物件排序
def dedupe(items): seen = set() for item in items: if item not in seen: yield item seen.add(item) #example a = [1,5,2,1,9,1,5,10] print list(dedupe(a)) def dedupe2(items, key = None): seen = set() for item in items: val = item if key is None else key(item) if val not in seen: yield item seen.add(val) #example a = [ {'x':1, 'y':2}, {'x':1, 'y':3}, {'x':1, 'y':2}, {'x':2, 'y':4}, ] print list( dedupe2(a, key=lambda d : (d['x'], d['y']) ) ) print list( dedupe2(a, key=lambda d : (d['x']) ) ) a = [1,5,2,1,9,1,5,10] print set(a)
items = [0,1,2,3,4,5,6] a = slice(2,4) print items[2:4] print items[a] items[a] = [10,11] print items print a.start print a.stop print a.step1.17 從字典中提取子集
words = [ 'look', 'into', 'my', 'eyes', 'look', 'into', 'my', 'eyes', 'the', 'look' ] from collections import Counter word_counts = Counter(words) top_three = word_counts.most_common(3) print top_three print word_counts['look'] print word_counts['the'] morewords = ['why', 'are', 'you', 'not', 'looking', 'in', 'my', 'eyes'] for word in morewords: word_counts[word] += 1 print word_counts['eyes'] print word_counts['why'] word_counts.update(morewords) print word_counts['eyes'] print word_counts['why'] a = Counter(words) b = Counter(morewords) print a print b c = a + b print c d = a - b print b
1.18 將名稱對應到序列的元素中
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} ] from operator import itemgetter rows_by_fname = sorted(rows, key=itemgetter('fname')) rows_by_uid = sorted(rows, key=itemgetter('uid')) print rows_by_fname print rows_by_uid rows_by_lfname = sorted(rows, key=itemgetter('lname', 'fname')) print rows_by_lfname rows_by_fname = sorted(rows, key=lambda r: r['fname']) rows_by_lfname = sorted(rows, key=lambda r: (r['fname'], r['lname'])) print rows_by_fname print rows_by_lfname print min(rows, key=itemgetter('uid')) print max(rows, key=itemgetter('uid'))
1.19 同時對資料進行轉換與換算
class User: def __init__(self, user_id): self.user_id = user_id def __repr__(self): return 'User({})'.format(self.user_id) users = [User(23), User(3), User(99)] print users print sorted(users, key = lambda u: u.user_id) from operator import attrgetter print sorted(users, key=attrgetter('user_id')) print min(users, key=attrgetter('user_id')) print max(users, key=attrgetter('user_id'))