Skip to Content
Python 技術手冊 第三版
book

Python 技術手冊 第三版

by lex Martelli, Anna Martelli Ravenscroft, Steve Holden
January 2018
Intermediate to advanced content levelIntermediate to advanced
856 pages
17h 17m
Chinese
GoTop Information, Inc.
Content preview from Python 技術手冊 第三版
argparse 模組
|
259
這個程序最好包裹到一個類別中,例如這樣:
class KeyHeap(object):
def __init__(self, alist, key):
self.heap = [
(key(o), i, o)
for i, o in enumerate(alist)]
heapq.heapify(self.heap)
self.key = key
if alist:
self.nexti = self.heap[-1][1] + 1
else:
self.nexti = 0
def __len__(self):
return len(self.heap)
def push(self, o):
heapq.heappush(
self.heap,
(self.key(o), self.nexti, o))
self.nexti += 1
def pop(self):
return heapq.heappop(self.heap)[-1]
在這個例子中,我們在經過裝飾的元組中間用了一個遞增的數字(在排序
鍵值之後,實際項目之前)以確保實際的項目永遠都不會被直接比較,即
使它們的排序鍵值相等(對
sort
與類似的函式來說,這個語意上的確保動
作是
key=
引數功能性重要的一個面向)。
argparse 模組
當你撰寫了一個專門要在命令列(command line)底下執行(或以類 Unix
系統中的「shell 指令稿」或者 Windows 中的「批次檔」形式執行)的 ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

測試驅動開發|使用 Python

測試驅動開發|使用 Python

Harry J.W. Percival
Linux 内核观测技术BPF

Linux 内核观测技术BPF

David Calavera, Lorenzo Fontana

Publisher Resources

ISBN: 9789864766819