最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Python入門教程(十四)Python的集合

 更新時(shí)間:2023年04月17日 10:42:33   作者:輕松學(xué)Python  
這篇文章主要介紹了Python入門教程(十四)Python的集合,Python是一門非常強(qiáng)大好用的語(yǔ)言,也有著易上手的特性,本文為入門教程,需要的朋友可以參考下

集合(Set)

集合是無(wú)序和無(wú)索引的集合。在 Python 中,集合用花括號(hào)編寫。

實(shí)例

創(chuàng)建集合:

thisset = {"apple", "banana", "cherry"}
print(thisset)

運(yùn)行實(shí)例

注釋:集合是無(wú)序的,因此您無(wú)法確定項(xiàng)目的顯示順序。

訪問項(xiàng)目

您無(wú)法通過引用索引來訪問 set 中的項(xiàng)目,因?yàn)?set 是無(wú)序的,項(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)建,就無(wú)法更改項(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 的長(zhǎng)度

要確定集合中有多少項(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 是無(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í)例

注釋:集合是無(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í)例

合并兩個(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)查看此頁(yè)面底部的集合方法完整列表。

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)文章

最新評(píng)論

延边| 射洪县| 商城县| 西和县| 建阳市| 乳山市| 镇康县| 大理市| 平江县| 广州市| 清丰县| 龙川县| 景德镇市| 隆德县| 甘孜| 文化| 正蓝旗| 清原| 阿瓦提县| 宝清县| 连云港市| 永吉县| 东光县| 澄迈县| 通化市| 涪陵区| 原阳县| 绍兴市| 衡水市| 平顶山市| 兰溪市| 紫云| 县级市| 安阳市| 南丰县| 大丰市| 长治县| 竹溪县| 错那县| 鸡泽县| 冷水江市|