Python數據結構(二)
array固定類型的數據序列,與list類似,只不過成員必須是相同的基本類型
array.
typecodes #包含所有可用類型代碼的字符串bBuhHiIlLqQfd
輸入代碼 | C型 | Python類型 | 最小字節數 | 筆記 |
---|---|---|---|---|
‘b‘ |
簽名字符 | INT | 1 | |
‘B‘ |
無符號字符 | INT | 1 | |
‘u‘ |
Py_UNICODE | Unicode字符 | 2 | (1) |
‘h‘ |
簽署簡稱 | INT | 2 | |
‘H‘ |
無符號短 | INT | 2 | |
‘i‘ |
簽名int | INT | 2 | |
‘I‘ |
無符號整數 | INT | 2 | |
‘l‘ |
長簽字 | INT | 4 | |
‘L‘ |
無符號長整數 | INT | 4 | |
‘q‘ |
簽了很久 | INT | 8 | (2) |
‘Q‘ |
無符號long long | INT | 8 | (2) |
‘f‘ |
浮動 | 浮動 | 4 | |
‘d‘ |
雙 | 浮動 | 8 |
import array import binascii s = ‘this is a ARRAY‘ w = [1,2,3,4] a = array.array(‘u‘,s)#實例化時候需要傳遞一個參數 b = array.array(‘i‘,w) print(a,binascii.hexlify(a),b,binascii.hexlify(b)) 結果: array(‘u‘, ‘this is a ARRAY‘) b‘740068006900730020006900730020006100200041005200520041005900‘ array(‘i‘, [1, 2, 3, 4]) b‘01000000020000000300000004000000‘
# 處理數組,類似其他的Python序列,可以采取同樣方式擴展和處理array a = array.array(‘i‘,range(3)) print(a) a.extend(range(3)) print(a,a[3:5],list(enumerate(a))) 結果: array(‘i‘, [0, 1, 2]) array(‘i‘, [0, 1, 2, 0, 1, 2]) array(‘i‘, [0, 1]) [(0, 0), (1, 1), (2, 2), (3, 0), (4, 1), (5, 2)]
heapq堆排序算法,堆(heap)是一種樹形數據結構,其中子節點與父節點是一種有序關系。
#創建堆 import heapq heap1 = [] for i in [19,9,4,10,11]:#元素逐個插入,形成堆結構 heapq.heappush(heap1,i) print(heap1) heap2 = [19,9,4,10,11] #目標已存在,直接轉化堆結構 heapq.heapify(heap2) print(heap2) for i in [0,13]: heapq.heapreplace(heap2,i) #維持固定大小的堆,每次插入一個元素同時刪除一個最小的元素 print(heap2) #heappop()刪除最小值元素 for i in range(len(heap1)): print(heapq.heappop(heap1),end=‘,‘) print()
結果: [4, 10, 9, 19, 11] [4, 9, 19, 10, 11] [9, 10, 19, 13, 11] 4,9,10,11,19,
bisect數組二等分算法,該模塊支持按排序順序維護列表,而無需在每次插入後對列表進行排序。對於昂貴的比較操作的長項目列表,這可能是比較常見的方法的改進。該模塊被稱為bisect
是因為它使用基本的二分算法來完成其工作,
import bisect import random
random.seek(1) l = [] for i in range(1,10): r = random.randint(1,100) position =bisect.bisect(l,r) bisect.insort(l,r) print(‘%3d%3d‘%(r,position),l) 結果:
18 0 [18]
73 1 [18, 73]
98 2 [18, 73, 98]
9 0 [9, 18, 73, 98]
33 2 [9, 18, 33, 73, 98]
16 1 [9, 16, 18, 33, 73, 98]
64 4 [9, 16, 18, 33, 64, 73, 98]
98 7 [9, 16, 18, 33, 64, 73, 98, 98]
58 4 [9, 16, 18, 33, 58, 64, 73, 98, 98]
處理重復:
bisect.
bisect_left
(a,x,lo = 0,hi = len(a))
bisect.
bisect_right
(a,x,lo = 0,hi = len(a))
bisect.
insort_left
(a,x,lo = 0,hi = len(a))
bisect.
insort_right
(a,x,lo = 0,hi = len(a))
import bisect import random random.seed(1) l = [] for i in range(1,10): r = random.randint(1,100) position =bisect.bisect_left(l,r) bisect.insort(l,r) print(‘%3d%3d‘%(r,position),l) 結果: 18 0 [18] 73 1 [18, 73] 98 2 [18, 73, 98] 9 0 [9, 18, 73, 98] 33 2 [9, 18, 33, 73, 98] 16 1 [9, 16, 18, 33, 73, 98] 64 4 [9, 16, 18, 33, 64, 73, 98] 98 6 [9, 16, 18, 33, 64, 73, 98, 98]#相同值放在了左邊 58 4 [9, 16, 18, 33, 58, 64, 73, 98, 98]
Queue 模塊提供一個適用與多線程編程先進先出FIFO數據結構,可以用來在生產者和消費者線程之間安全的傳遞消息或者數據,為調用者處理鎖定,使多個線程可以安全的處理同一個Queue實例
#基本的FIFO import queue q = queue.Queue() for i in range(5): q.put(i) #元素添加 while not q.empty(): print(q.get())#元素刪除 結果 0 1 2 3 4 #LIFO隊列,LifoQueue使用了後進先出(LIFO)順序,通常與棧結構關聯 import queue q=queue.LifoQueue() for i in range(5): q.put(i) #元素添加 while not q.empty(): print(q.get())#元素刪除 結果: 4 3 2 1 0 #優先隊列,PriorityQueue使用隊列內容的有序順序來決定獲取哪個元素 import queue import threading class Job(): def __init__(self,priority,description): self.priority = priority self.description = description print(‘new job‘,description) def __lt__(self, other): return self.priority < other.priority q = queue.PriorityQueue() q.put(Job(3,‘mid-level job‘)) q.put(Job(10,‘low-level job‘)) q.put(Job(1,‘important-level job‘)) def process_job(q): while True: next_job = q.get() print(‘Processing job:‘,next_job.description) q.task_done() #用task_done()以指示該項目已被檢索並且所有工作都已完成,計數就會減少 workers = [threading.Thread(target=process_job,args=(q,)), threading.Thread(target=process_job,args=(q,)), ] for w in workers: print(w.getName()) w.setDaemon(True) w.start() q.join()#阻塞,直到隊列中的所有項目都被獲取並處理 結果: new job mid-level job new job low-level job new job important-level job Thread-1 Processing job: important-level job Thread-2 Processing job: mid-level job Processing job: low-level job
struct 二進制數據結構,將字節解釋為二進制數據,在字符串和二進制數據之間進行轉換
weakref 對象的非永久引用
Python數據結構(二)