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 技術手冊 第三版
集合運算
|
79
Python
標準程式庫模組
operator
(涵蓋於後面的「operator 模組」中)提供
attrgetter
itemgetter
這兩個高階函式(higher-order functions),它們
所產生的函式專門用於串列方法
sort
和內建函式
sorted
key=
選擇性引
數。完全相同的選擇性引數
key=
也出現在內建函式
min
max
中,以及標
準程式庫模組
heapq
(涵蓋於後面的「heapq 模組」中)的函式
nsmallest
nlargest
中。
集合運算
Python 提供各式各樣能夠套用到集合(普通的或凍結的都有)上的運算。
既然集合(sets)是容器,內建函式
len
就能接受一個集合作為其單一引
數,並回傳集合中項目的數量。集合是可迭代的(iterable),所以你可以
將它傳給接受 iterable 作為引數的任何函式或方法。在這種情況下,迭代
iteration)的動作會以某種任意的順序產出(yield)集合中的項目。舉例
來說,對於任何的集合
S
min(S)
會回傳
S
中最小的項目,因為帶有單一
引數的
min
會迭代(iterate)那個引數(在此順序並不重要,因為隱含的比
較動作有遞移性)。
集合的成員資格
k in S
運算子會檢查物件
k
是否為集合
S
中的一個項目。若是,它就會回
True
,否則回傳
False
k not in S
等同於
not (k in S)
集合方法
集合物件提供了數個方法,如表 3-4 中所示。非變動方法會回傳一個
結果但不會更動套用它們的物件
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