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

Python 技術手冊 第三版

by lex Martelli, Anna Martelli Ravenscroft, Steve Holden
January 2018
Intermediate 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.

Read now

Unlock full access

More than 5,000 organizations count on O’Reilly

AirBnbBlueOriginElectronic ArtsHomeDepotNasdaqRakutenTata Consultancy Services

QuotationMarkO’Reilly covers everything we've got, with content to help us build a world-class technology community, upgrade the capabilities and competencies of our teams, and improve overall team performance as well as their engagement.
Julian F.
Head of Cybersecurity
QuotationMarkI wanted to learn C and C++, but it didn't click for me until I picked up an O'Reilly book. When I went on the O’Reilly platform, I was astonished to find all the books there, plus live events and sandboxes so you could play around with the technology.
Addison B.
Field Engineer
QuotationMarkI’ve been on the O’Reilly platform for more than eight years. I use a couple of learning platforms, but I'm on O'Reilly more than anybody else. When you're there, you start learning. I'm never disappointed.
Amir M.
Data Platform Tech Lead
QuotationMarkI'm always learning. So when I got on to O'Reilly, I was like a kid in a candy store. There are playlists. There are answers. There's on-demand training. It's worth its weight in gold, in terms of what it allows me to do.
Mark W.
Embedded Software Engineer

You might also like

利用 Python 进行数据分析(原书第2版)

利用 Python 进行数据分析(原书第2版)

Wes McKinney

Publisher Resources

ISBN: 9789864766819