欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Python入門之集合的使用教程

 更新時(shí)間:2022年09月06日 09:25:50   作者:胡安民-獨(dú)行者  
在Python中,集合(Set)?是一個(gè)無序、不重復(fù)的序列,它不支持索引。本文將通過示例為大家詳細(xì)講講Python中集合是使用,需要的可以參考一下

前序

在Python中,集合(Set) 是一個(gè)無序、不重復(fù)的序列,它不支持索引。集合一般用于元素去重或者一些數(shù)學(xué)中的操作像union,intersection,difference和complement等數(shù)學(xué)運(yùn)算。

集合操作

創(chuàng)建集合

通過使用set()函數(shù)或?qū)⑺性胤胖迷谝粚?duì)大括號(hào)內(nèi)創(chuàng)建一個(gè)集合。

Days=set(["Mon","Tue","Wed","Thu","Fri","Sat","Sun"])
Months={"Jan","Feb","Mar"}
Dates={21,22,17}

訪問集合

我們無法訪問集合中的單個(gè)值。我們只能如上所示訪問所有元素。但是我們也可以通過遍歷該集合來獲取單個(gè)元素的列表。

Days=set(["Mon","Tue","Wed","Thu","Fri","Sat","Sun"])
for d in Days:
    print(d)

判斷集合是存在指定值

    x = {"a","b","c"}
    print("a" in x)

集合添加

我們可以使用add()方法將元素添加到集合中。

Days=set(["Mon","Tue","Wed","Thu","Fri","Sat"])
Days.add("Sun")
print(Days)

使用 update() 方法將多個(gè)項(xiàng)添加到集合中:

thisset = {"apple", "banana", "cherry"}
thisset.update(["orange", "mango", "grapes"])
print(thisset)

集合刪除

要?jiǎng)h除集合中的項(xiàng)目,請(qǐng)使用 remove() 或 discard() 方法。。

Days=set(["Mon","Tue","Wed","Thu","Fri","Sat"])
Days.discard("Sun")
print(Days)
thisset = {"apple", "banana", "cherry"}
thisset.remove("banana")
print(thisset)

注釋:如果要?jiǎng)h除的項(xiàng)目不存在,則 remove() 將引發(fā)錯(cuò)誤。 如果要?jiǎng)h除的項(xiàng)目不存在,則 discard() 不會(huì)引發(fā)錯(cuò)誤。

您還可以使用 pop() 方法刪除項(xiàng)目,但此方法將刪除最后一項(xiàng)。請(qǐng)記住,set 是無序的,因此您不會(huì)知道被刪除的是什么項(xiàng)目。 pop() 方法的返回值是被刪除的項(xiàng)目。

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

clear() 方法清空集合:

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

del 徹底刪除集合:

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

集合函數(shù)

返回集合的副本

如果不使用這種方式的話,那么直接用=號(hào)復(fù)制的是地址,這樣就導(dǎo)致了修改了值而原集合也會(huì)發(fā)生變化

fruits = {"apple", "banana", "cherry"}
x = fruits.copy()
print(x)

判斷集合內(nèi)是否包含另一個(gè)集合

如果指定集中的所有項(xiàng)目都存在于原始集中,則 issuperset() 方法將返回 True,否則返回 False。

x = {"f", "e", "d", "c", "b", "a"}
y = {"a", "b", "c"}
z = x.issuperset(y) 
print(z)

并集

您可以使用 union() 方法返回包含兩個(gè)集合中所有項(xiàng)目的新集合,也可以使用 update() 方法將一個(gè)集合中的所有項(xiàng)目插入另一個(gè)集合中:

set1 = {"a", "b" , "c"}
set2 = {1, 2, 3}

set3 = set1.union(set2)
print(set3)
set1 = {"a", "b" , "c"}
set2 = {1, 2, 3}

set1.update(set2)
print(set1)

注釋:union() 和 update() 都將排除任何重復(fù)項(xiàng)。

差集

返回x集合有的y集合沒有的

x = {"apple", "banana", "cherry"}
y = {"google", "microsoft", "apple"}

z = x.difference(y) 

print(z)

交集

intersection() 方法返回包含兩個(gè)或更多集合之間都存在的項(xiàng)目

x = {"apple", "banana", "cherry"}
y = {"google", "microsoft", "apple"}

z = x.intersection(y) 

print(z)

補(bǔ)集

返回的集合包含兩個(gè)集合中都不存在的項(xiàng)的混合體 ,也就是a和b集合都有的不輸出

x = {"apple", "banana", "cherry"}
y = {"google", "microsoft", "apple"}

z = x.symmetric_difference(y) 

print(z)

交集判斷

集合 x 中沒有項(xiàng)目存在于集合 y 中,則返回 True 否則返回False

x = {"apple", "banana", "cherry"}
y = {"google", "microsoft", "facebook"}
z = x.isdisjoint(y) 
print(z)

到此這篇關(guān)于Python入門之集合的使用教程的文章就介紹到這了,更多相關(guān)Python集合內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論