1. Python提供靈活的數據解包操做,若是想忽略某些值能夠用下劃線代替"_",僅能表明一個值python
>>>p = (4, 5) >>>x, y = p >>>x 4 >>>y 5 >>> data = [ 'ACME', 50, 91.1, (2012, 12, 21) ] >>> name, shares, price, date = data >>> name 1'ACME' >>> date (2012, 12, 21) >>> data = [ 'ACME', 50, 91.1, (2012, 12, 21) ] >>> _, shares, price, _ = data >>> shares 50 >>> price 91.1
2若是想表明多個值,則需在變量明前加"*",該變量自動爲list,僅在python3中支持.算法
>>> record = ('Dave', 'dave@example.com', '773-555-1212', '847-555-1212') >>> name, email, *phone_numbers = user_record >>> name 'Dave' >>> email 'dave@example.com' >>> phone_numbers ['773-555-1212', '847-555-1212'] #only python3 >>> a=[1,2,3] >>> b,c,*d=a File "<stdin>", line 1 b,c,*d=a ^ SyntaxError: invalid syntax 效果相似與傳參時的可變參數 def calc(*numbers): sum = 0 for n in numbers: sum = sum + n * n return sum
3只保留最新的N條記錄,api
deque建立了一個固定尺寸的隊列,append向隊列的右端添加,左端刪除,也能夠用appendleft()左添,右端刪除,若是不指定maxlen,效果和list類似.在兩邊添加或pop的複雜度都是O(1),而list在左端添加或刪除的複雜度是O(N)數據結構
from collections import deque >>> q = deque(maxlen=3) >>> q.append(1) >>> q.append(2) >>> q.append(3) >>> q deque([1, 2, 3], maxlen=3) >>> q.append(4) >>> q deque([2, 3, 4], maxlen=3) >>> q.append(5) >>> q deque([3, 4, 5], maxlen=3) >>>q.appendleft(7) >>>q deque([7,3, 4], maxlen=3)
4查找最大或最小的N個元素app
heapq有有兩個功能,nlargest() 和 nsmallest(),分別是找最大或最小的N個元素,heapq模塊實現了一個適用於Python列表的最小堆排序算法。 堆(heap)是一個樹形數據結構,其中子節點與父節點是一種有序關係。老是知足a[k] <= a[2*k+1] ,a[k] <= a[2*k+2],堆頂元素 a[0] 永遠是最小的.
spa
import heapq nums = [1, 8, 2, 23, 7, -4, 18, 23, 42, 37, 2] print(heapq.nlargest(3, nums)) # Prints [42, 37, 23] print(heapq.nsmallest(3, nums)) # Prints [-4, 1, 2] 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} ] cheap = heapq.nsmallest(3, portfolio, key=lambda s: s['price']) #heapq還提供了另外一種方法 >>> nums = [1, 8, 2, 23, 7, -4, 18, 23, 42, 37, 2] >>> heapq.heapify(num)#首先要heap化,生成樹形結構 >>> num [-4, 2, 1, 23, 7, 2, 18, 23, 42, 37, 8] >>> heapq.heappop(num) -4 >>> heapq.heappop(num) 1 >>> heapq.heappop(num) 2
若是你只是想找最小值,最大值,min(),max()更合適.若是N接近數據的len(),sorted(items)[:N] 或 sorted(items)[-N:] )是更合適的選擇code
5實現優先級隊列,能夠利用heapq.heappush向list中添加一個list或tuple(list優先級比tuple低),list[0]或tuple[0]指定優先級,實際至關於比較大小的過程,list[0]小的優先級低,先刪除.排序
>>> heapq.heappush(queue,[1,2]) >>> heapq.heappush(queue,[1,3]) >>> heapq.heappush(queue,[2,3]) >>> heapq.heappush(queue,[3,3]) >>> heapq.heappush(queue,(1,2)) >>> heapq.heappush(queue,(1,3)) >>> heapq.heappush(queue,(2,3)) >>> heapq.heappush(queue,(3,3)) >>> queue [[1, 2], [1, 3], [2, 3], [3, 3], (1, 2), (1, 3), (2, 3), (3, 3)] >>> heapq.heappop(queue) [1, 2] >>> heapq.heappop(queue) [1, 3] >>> heapq.heappop(queue) [2, 3] >>> heapq.heappop(queue) [3, 3] >>> heapq.heappop(queue) (1, 2) >>> heapq.heappop(queue) (1, 3) >>> heapq.heappop(queue) (2, 3) >>> heapq.heappop(queue) (3, 3) heapq其餘方法 heapq.heapreplace(heap,item) #刪除最小元素值,添加新的元素值 >>>heap=[2, 4, 3, 5, 7, 8,9, 6] >>>heapq.heapreplace(heap,11) 2 >>> heap [3, 4, 8, 5, 7, 11, 9, 6] heapq.heappushpop(heap,item) #首判斷添加元素值與堆的第一個元素值對比,若是大於則刪除最小元素,而後 #添加新的元素值,不然不更改堆 >>> heapq.heappushpop(heap,2) 2 >>> heap [3, 4, 8, 5, 7, 11, 9, 6] >>> heapq.heappushpop(heap,4) 3 >>> heap [4, 4, 8, 5, 7, 11, 9, 6] heapq.merge(...) #將多個堆合併