您好,登錄后才能下訂單哦!
本篇內(nèi)容主要講解“Python集合是什么及怎么創(chuàng)建”,感興趣的朋友不妨來(lái)看看。本文介紹的方法操作簡(jiǎn)單快捷,實(shí)用性強(qiáng)。下面就讓小編來(lái)帶大家學(xué)習(xí)“Python集合是什么及怎么創(chuàng)建”吧!
集合是無(wú)序和無(wú)索引的集合。在 Python 中,集合用花括號(hào)編寫(xiě)。
實(shí)例
創(chuàng)建集合:
thisset = {"apple", "banana", "cherry"} print(thisset)
運(yùn)行實(shí)例
注釋?zhuān)杭鲜菬o(wú)序的,因此您無(wú)法確定項(xiàng)目的顯示順序。
您無(wú)法通過(guò)引用索引來(lái)訪(fǎng)問(wèn) set 中的項(xiàng)目,因?yàn)?set 是無(wú)序的,項(xiàng)目沒(méi)有索引。
但是您可以使用 for 循環(huán)遍歷 set 項(xiàng)目,或者使用 in 關(guān)鍵字查詢(xú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í)例
集合一旦創(chuàng)建,就無(wú)法更改項(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í)例
要確定集合中有多少項(xiàng),請(qǐng)使用 len() 方法。
實(shí)例
獲取集合中的項(xiàng)目數(shù):
thisset = {"apple", "banana", "cherry"} print(len(thisset))
運(yùn)行實(shí)例
要?jiǎng)h除集合中的項(xiàng)目,請(qǐng)使用 remove() 或 discard() 方法。
實(shí)例
使用 remove() 方法來(lái)刪除 “banana”:
thisset = {"apple", "banana", "cherry"} thisset.remove("banana") print(thisset)
運(yùn)行實(shí)例
注釋?zhuān)喝绻獎(jiǎng)h除的項(xiàng)目不存在,則 remove() 將引發(fā)錯(cuò)誤。
實(shí)例
使用 discard() 方法來(lái)刪除 “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 是無(wú)序的,因此您不會(huì)知道被刪除的是什么項(xiàng)目。
pop() 方法的返回值是被刪除的項(xiàng)目。
實(shí)例
使用 pop() 方法刪除最后一項(xiàng):
thisset = {"apple", "banana", "cherry"} x = thisset.pop() print(x) print(thisset)
運(yùn)行實(shí)例
注釋?zhuān)杭鲜菬o(wú)序的,因此在使用 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í)例
在 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è)集合連接起來(lái),并且僅保留重復(fù)項(xiàng),或者永遠(yuǎn)不保留重復(fù)項(xiàng),請(qǐng)查看此頁(yè)面底部的集合方法完整列表。
也可以使用 set() 構(gòu)造函數(shù)來(lái)創(chuàng)建集合。
實(shí)例
使用 set() 構(gòu)造函數(shù)來(lái)創(chuàng)建集合:
thisset = set(("apple", "banana", "cherry")) # 請(qǐng)留意這個(gè)雙括號(hào) print(thisset)
運(yùn)行實(shí)例
Python 擁有一套能夠在集合(set)上使用的內(nèi)建方法。
到此,相信大家對(duì)“Python集合是什么及怎么創(chuàng)建”有了更深的了解,不妨來(lái)實(shí)際操作一番吧!這里是億速云網(wǎng)站,更多相關(guān)內(nèi)容可以進(jìn)入相關(guān)頻道進(jìn)行查詢(xún),關(guān)注我們,繼續(xù)學(xué)習(xí)!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀(guān)點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。