Python的集合類型之set和frozenset詳解
集合類型— set, frozenset
set 對象是由具有唯一性的hashable 對象所組成的無序多項集。常見的用途包括成員檢測、從序列中去除重復(fù)項以及數(shù)學中的集合類計算,例如交集、并集、差集與對稱差集等等
兩個類的構(gòu)造器具有相同的作用方式:
- class set([iterable ])
- class frozenset([iterable ])
集合可用多種方式來創(chuàng)建:
- 使用花括號內(nèi)以逗號分隔元素的方式: {‘jack’, ‘sjoerd’}
- 使用集合推導(dǎo)式: {c for c in ‘abracadabra’ if c not in ‘abc’}
- 使用類型構(gòu)造器: set(), set(‘foobar’), set([‘a’, ‘b’, ‘foo’])
set 和frozenset 的實例提供以下操作:
len(s)
計算集合 s 元素個數(shù)
x in s
檢測x是否為s中的成員
x not in s
檢測x 是否非s 中的成員
isdisjoint(other)
用于判斷兩個集合是否包含相同的元素,如果沒有返回 True,否則返回 False
x = {"apple", "banana", "cherry"}
y = {"google", "runoob", "facebook"}
z = x.isdisjoint(y)
print(z)
issubset(other)
用于判斷集合的所有元素是否都包含在指定集合中,如果是則返回 True,否則返回 False
x = {"a", "b", "c"}
y = {"f", "e", "d", "c", "b", "a"}
z = x.issubset(y)
issuperset(other)
用于判斷指定集合的所有元素是否都包含在原始的集合中,如果是則返回 True,否則返回 False。
x = {"f", "e", "d", "c", "b", "a"}
y = {"a", "b", "c"}
z = x.issuperset(y)
print(z)
union(*others)
返回兩個集合的并集,即包含了所有集合的元素,重復(fù)的元素只會出現(xiàn)一次
x = {"apple", "banana", "cherry"}
y = {"google", "runoob", "apple"}
z = x.union(y)
print(z)
intersection(*others)
用于返回兩個或更多集合中都包含的元素,即交集。
x = {"apple", "banana", "cherry"}
y = {"google", "runoob", "apple"}
z = x.intersection(y)
print(z)
difference(*others)
用于返回集合的差集,即返回的集合元素包含在第一個集合中,但不包含在第二個集合(方法的參數(shù))中。
x = {"apple", "banana", "cherry"}
y = {"google", "microsoft", "apple"}
z = x.difference(y)
print(z)
symmetric_difference(other)
返回兩個集合中不重復(fù)的元素集合,即會移除兩個集合中都存在的元素。
x = {"apple", "banana", "cherry"}
y = {"google", "runoob", "apple"}
z = x.symmetric_difference(y)
print(z)
copy()
用于拷貝一個集合。
sites = {"Google", "Runoob", "Taobao"}
x = sites.copy()
print(x)
可用于set 而不能用于不可變的frozenset 實例的操作:
update(*others)
用于修改當前集合,可以添加新的元素或集合到當前集合中,如果添加的元素在集合中已存在,則該元素只會出現(xiàn)一次,重復(fù)的會忽略。
x = {"apple", "banana", "cherry"}
y = {"google", "runoob", "apple"}
x.update(y)
print(x)
intersection_update(*others)
- intersection_update() 方法用于獲取兩個或更多集合中都重疊的元素,即計算交集。
- intersection_update() 方法不同于 intersection() 方法,因為 intersection() 方法是返回一個新的集合,而 intersection_update() 方法是在原始的集合上移除不重疊的元素。
x = {"apple", "banana", "cherry"} # y 集合不包含 banana 和 cherry,被移除
y = {"google", "runoob", "apple"}
x.intersection_update(y)
print(x)
difference_update(*others)
- difference_update() 方法用于移除兩個集合中都存在的元素。
- difference_update() 方法與 difference() 方法的區(qū)別在于 difference() 方法返回一個移除相同元素的新集合,而 difference_update() 方法是直接在原來的集合中移除元素,沒有返回值。
x = {"apple", "banana", "cherry"}
y = {"google", "microsoft", "apple"}
x.difference_update(y)
print(x)
symmetric_difference_update(other)
symmetric_difference_update() 方法移除當前集合中在另外一個指定集合相同的元素,并將另外一個指定集合中不同的元素插入到當前集合中
x = {"apple", "banana", "cherry"}
y = {"google", "runoob", "apple"}
x.symmetric_difference_update(y)
print(x)
add(elem)
用于給集合添加元素,如果添加的元素在集合中已存在,則不執(zhí)行任何操作。
fruits = {"apple", "banana", "cherry"}
fruits.add("orange")
print(fruits)
remove(elem)
用于移除集合中的指定元素。
fruits = {"apple", "banana", "cherry"}
fruits.remove("banana")
print(fruits)
discard(elem)
如果元素elem 存在于集合中則將其移除
fruits = {"apple", "banana", "cherry"}
fruits.discard("banana")
print(fruits)
pop()
從集合中移除并返回任意一個元素。如果集合為空則會引發(fā)KeyError。
fruits = {"apple", "banana", "cherry"}
fruits.pop()
print(fruits)
clear()
用于移除集合中的所有元素。
fruits = {"apple", "banana", "cherry"}
fruits.clear()
print(fruits)
關(guān)系運算
s_1024 = {"佩奇","老男孩","海峰","馬JJ","老村長","黑姑娘","Alex"}
s_pornhub = {"Alex","Egon","Rain","馬JJ","Nick","Jack"}
print(s_1024 & s_pornhub) # 交集, elements in both set
print(s_1024 | s_pornhub) # 并集 or 合集
print(s_1024 - s_pornhub) # 差集 , only in 1024
print(s_pornhub - s_1024) # 差集, only in pornhub
print(s_1024 ^ s_pornhub) # 對稱差集, 把腳踩2只船的人T出去
總結(jié)
本篇文章就到這里了,希望能夠給你帶來幫助,也希望您能夠多多關(guān)注腳本之家的更多內(nèi)容!
相關(guān)文章
python數(shù)學模塊(math/decimal模塊)
這篇文章主要介紹了python數(shù)學模塊(math/decimal模塊),文章圍繞主題展開詳細的內(nèi)容介紹,具有一定的參考價值,需要的小伙伴可以參考一下2022-09-09
淺談keras保存模型中的save()和save_weights()區(qū)別
這篇文章主要介紹了淺談keras保存模型中的save()和save_weights()區(qū)別,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-05-05
python 3.6 +pyMysql 操作mysql數(shù)據(jù)庫(實例講解)
下面小編就為大家分享一篇python 3.6 +pyMysql 操作mysql數(shù)據(jù)庫的實例講解,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2017-12-12

