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 技術手冊 第三版
字典運算
|
81
Python
while S:
item = S.pop()
... 處理項目 ...
的破壞性迴圈(destructive loop)與這樣:
for item in S:
... 處理項目 ...
的非破壞性迴圈(nondestructive loop)比起來的潛在好處是,在破壞性迴
圈的主體(body)中,你能夠修改
S
(新增或移除項目),而那在非破壞性
迴圈中是不被允許的。
集合也有名為
difference_update
intersection_update
symmetric_
difference_update
update
(對應非變動方法
union
)的變動方法。這
幾個變動方法進行的運算與它們對應的非變動方法相同,不過它們是在
原處(in place)進行那些運算,更動你在其上呼叫該方法的集合,並
回傳
None
。那四個對應的非變動方法也能夠以運算子語法來取用,分
是:
S-S2
S&S2
S^S2
S|S2
,其中
S2
是一個集合(set)或凍結集
frozenset)。而它們對應的變動方法則可以使用擴增指定(augmented
assignment)語法來取用,分別是:
S-=S2
S&=S2
S^=S2
S|=S2
。請注
意,當你使用運算子或擴增指定語法,那麼兩個運算元(operands)都必
須是集合或凍結集,然而,如果你是呼叫具名的方法,引數
S1
可以是帶有
可雜湊項目的任何 iterable,而它們運作起來就好像你傳入的是
set(S1)
字典運算
Python 提供各式各樣能夠套用到字典( ...
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