Python入門教程(十四)Python的集合
集合(Set)
集合是無序和無索引的集合。在 Python 中,集合用花括號(hào)編寫。
實(shí)例
創(chuàng)建集合:
thisset = {"apple", "banana", "cherry"}
print(thisset)
運(yùn)行實(shí)例

注釋:集合是無序的,因此您無法確定項(xiàng)目的顯示順序。
訪問項(xiàng)目
您無法通過引用索引來訪問 set 中的項(xiàng)目,因?yàn)?set 是無序的,項(xiàng)目沒有索引。
但是您可以使用 for 循環(huán)遍歷 set 項(xiàng)目,或者使用 in 關(guān)鍵字查詢集合中是否存在指定值。
實(shí)例
遍歷集合,并打印值:
thisset = {"apple", "banana", "cherry"}
for x in thisset:
print(x)
運(yùn)行實(shí)例

實(shí)例
檢查 set 中是否存在 “banana”:
thisset = {"apple", "banana", "cherry"}
print("banana" in thisset)
運(yùn)行實(shí)例
更改項(xiàng)目
集合一旦創(chuàng)建,就無法更改項(xiàng)目,但是可以添加新項(xiàng)目。
添加項(xiàng)目
要將一個(gè)項(xiàng)添加到集合,請(qǐng)使用 add() 方法。
要向集合中添加多個(gè)項(xiàng)目,請(qǐng)使用 update() 方法。
實(shí)例
使用 add() 方法向 set 添加項(xiàng)目:
thisset = {"apple", "banana", "cherry"}
thisset.add("orange")
print(thisset)
運(yùn)行實(shí)例

實(shí)例
使用 update() 方法將多個(gè)項(xiàng)添加到集合中:
thisset = {"apple", "banana", "cherry"}
thisset.update(["orange", "mango", "grapes"])
print(thisset)
運(yùn)行實(shí)例

獲取 Set 的長度
要確定集合中有多少項(xiàng),請(qǐng)使用 len() 方法。
實(shí)例
獲取集合中的項(xiàng)目數(shù):
thisset = {"apple", "banana", "cherry"}
print(len(thisset))
運(yùn)行實(shí)例

刪除項(xiàng)目
要?jiǎng)h除集合中的項(xiàng)目,請(qǐng)使用 remove() 或 discard() 方法。
實(shí)例
使用 remove() 方法來刪除 “banana”:
thisset = {"apple", "banana", "cherry"}
thisset.remove("banana")
print(thisset)
運(yùn)行實(shí)例

注釋:如果要?jiǎng)h除的項(xiàng)目不存在,則 remove() 將引發(fā)錯(cuò)誤。
實(shí)例
使用 discard() 方法來刪除 “banana”:
thisset = {"apple", "banana", "cherry"}
thisset.discard("banana")
print(thisset)
運(yùn)行實(shí)例

注釋:如果要?jiǎng)h除的項(xiàng)目不存在,則 discard() 不會(huì)引發(fā)錯(cuò)誤。
還可以使用 pop() 方法刪除項(xiàng)目,但此方法將刪除最后一項(xiàng)。請(qǐng)記住,set 是無序的,因此您不會(huì)知道被刪除的是什么項(xiàng)目。
pop() 方法的返回值是被刪除的項(xiàng)目。
實(shí)例
使用 pop() 方法刪除最后一項(xiàng):
thisset = {"apple", "banana", "cherry"}
x = thisset.pop()
print(x)
print(thisset)
運(yùn)行實(shí)例

注釋:集合是無序的,因此在使用 pop() 方法時(shí),您不會(huì)知道刪除的是哪個(gè)項(xiàng)目。
實(shí)例
clear() 方法清空集合:
thisset = {"apple", "banana", "cherry"}
thisset.clear()
print(thisset)
運(yùn)行實(shí)例

實(shí)例
del 徹底刪除集合:
thisset = {"apple", "banana", "cherry"}
del thisset
print(thisset)
運(yùn)行實(shí)例

合并兩個(gè)集合
在 Python 中,有幾種方法可以連接兩個(gè)或多個(gè)集合。
可以使用 union() 方法返回包含兩個(gè)集合中所有項(xiàng)目的新集合,也可以使用 update() 方法將一個(gè)集合中的所有項(xiàng)目插入另一個(gè)集合中:
實(shí)例
union() 方法返回一個(gè)新集合,其中包含兩個(gè)集合中的所有項(xiàng)目:
set1 = {"a", "b" , "c"}
set2 = {1, 2, 3}
set3 = set1.union(set2)
print(set3)
運(yùn)行實(shí)例

實(shí)例
update() 方法將 set2 中的項(xiàng)目插入 set1 中:
set1 = {"a", "b" , "c"}
set2 = {1, 2, 3}
set1.update(set2)
print(set1)
運(yùn)行實(shí)例

注釋:union() 和 update() 都將排除任何重復(fù)項(xiàng)。
還有其他方法將兩個(gè)集合連接起來,并且僅保留重復(fù)項(xiàng),或者永遠(yuǎn)不保留重復(fù)項(xiàng),請(qǐng)查看此頁面底部的集合方法完整列表。
set() 構(gòu)造函數(shù)
也可以使用 set() 構(gòu)造函數(shù)來創(chuàng)建集合。
實(shí)例
使用 set() 構(gòu)造函數(shù)來創(chuàng)建集合:
thisset = set(("apple", "banana", "cherry")) # 請(qǐng)留意這個(gè)雙括號(hào)
print(thisset)
運(yùn)行實(shí)例

Set 方法
Python 擁有一套能夠在集合(set)上使用的內(nèi)建方法。

到此這篇關(guān)于Python入門教程(十四)Python的集合的文章就介紹到這了,更多相關(guān)Python 集合內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
python繪制已知點(diǎn)的坐標(biāo)的直線實(shí)例
今天小編就為大家分享一篇python繪制已知點(diǎn)的坐標(biāo)的直線實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2019-07-07
Python開發(fā)如何在ubuntu 15.10 上配置vim
這篇文章主要介紹了Python開發(fā)如何在ubuntu 15.10 上配置vim 的相關(guān)資料,需要的朋友可以參考下2016-01-01
Python程序設(shè)計(jì)入門(5)類的使用簡(jiǎn)介
這篇文章主要介紹了Python類的使用,需要的朋友可以參考下2014-06-06
Pycharm中使用git進(jìn)行合作開發(fā)的教程詳解
這篇文章主要介紹了Pycharm中使用git進(jìn)行合作開發(fā),本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-11-11

