人妖在线一区,国产日韩欧美一区二区综合在线,国产啪精品视频网站免费,欧美内射深插日本少妇

新聞動態(tài)

Python集合的基礎(chǔ)操作

發(fā)布日期:2021-12-16 19:28 | 文章來源:源碼中國

1、集合

Python中的集合和數(shù)學上的集合基本是沒有區(qū)別的,是無序的,即不可以使用索引訪問的,集合中是不能出現(xiàn)重復元素的。

2、創(chuàng)建集合

Python中創(chuàng)建集合使用花括號{}字面量的方式,或者使用set()來創(chuàng)建一個集合。使用{}中必須至少含一個元素,創(chuàng)建空集合不能使用{},這樣創(chuàng)建的是空字典,應該使用set()的方式來創(chuàng)建一個集合。

# 使用字面量方法創(chuàng)建集合
set1 = {"Hello", "World"}
print(type(set1), set1)  # <class 'set'> {'World', 'Hello'}
# print(set1[1]) # TypeError: 'set' object does not support indexing
# 使用set()的方式創(chuàng)建
set2 = set("hello")  # {'l', 'o', 'h', 'e'}集合會去掉重復的元素,即"l"
print(set2)
# 創(chuàng)建空集合
set3 = {}
set4 = set()
print(type(set3), type(set4))  # <class 'dict'>字典 <class 'set'>
list1 = [1, 2, 3, 4, 3, 5, 1, 3]
# 將列表轉(zhuǎn)換為集合
set6 = set(list1)
print(type(set6), set6)  # <class 'set'> {1, 2, 3, 4, 5}
# 使用生成列生成列表
set5 = {num for num in range(20) if num % 3 == 0}
print(set5)  # {0, 3, 6, 9, 12, 15, 18}
# 遍歷循環(huán)
for ch in set5:
 print(ch)

注意:集合中的元素為不可變的類型,如整數(shù)、浮點、字符串、元組等,也就是說可變類型不能作為元組的元素,集合本身也是可變類型,所以集合不能夠作為集合中的元素。

3、集合的運算

集合數(shù)據(jù)類型擁有超多的運算符,包括成員運算、交集運算、并集運算、差集運算、比較運算(相等性、子集、超集)等。

3.1 成員運算

可以通過成員運算innot in 檢查元素是否在集合中,

示例代碼如下:

set1 = {"你好", "Python", "這個是", "集合", "set"}
print("Python" in set1)  # True
print("您好" in set1)  # False
print("set" not in set1)  # False
print("list" not in set1)  # True

3.2 交并差運算

Python中的集合跟數(shù)學上的集合一樣,可以進行交集、并集、差集等運算,而且可以通過運算符和方法調(diào)用兩種方式來進行操作。

示例代碼如下:

set1 = {1, 2, 3, 4, 5, 6, 7}
set2 = {2, 4, 6, 8, 10}

# 交集(咱倆都有的)
# 方法一: 使用 & 運算符
print(set1 & set2) # {2, 4, 6}
# 方法二: 使用intersection方法
print(set1.intersection(set2)) # {2, 4, 6}
# 并集(咱倆加在一起)
# 方法一: 使用 | 運算符
print(set1 | set2)# {1, 2, 3, 4, 5, 6, 7, 8, 10}
# 方法二: 使用union方法
print(set1.union(set2)) # {1, 2, 3, 4, 5, 6, 7, 8, 10}
# 差集(我不要你有的)
# 方法一: 使用 - 運算符
print(set1 - set2)  # {1, 3, 5, 7}
# 方法二: 使用difference方法
print(set1.difference(set2)) # {1, 3, 5, 7}
# 對稱差(咱倆不要你的和我的,只要專一的)
# 方法一: 使用 ^ 運算符
print(set1 ^ set2)# {1, 3, 5, 7, 8, 10}
# 方法二: 使用symmetric_difference方法
print(set1.symmetric_difference(set2)) # {1, 3, 5, 7, 8, 10}
# 方法三: 對稱差相當于兩個集合的并集減去交集
print((set1 | set2) - (set1 & set2))# {1, 3, 5, 7, 8, 10}

集合的交集、并集、差集運算還可以跟賦值運算一起構(gòu)成復合運算,

示例代碼如下:

set1 = {1, 3, 5, 7}
set2 = {2, 4, 6}
# 將set1和set2求并集再賦值給set1
# 也可以通過set1.update(set2)來實現(xiàn)
set1 |= set2  # set1 = set1 | set2
print(set1) # {1, 2, 3, 4, 5, 6, 7}
set3 = {3, 6, 9}
# 將set1和set3求交集再賦值給set1
# 也可以通過set1.intersection_update(set3)來實現(xiàn)
set1 &= set3  # set1 = set1 & set3
print(set1) # {3, 6}

3.3 比較運算

兩個集合可以用==和!=進行相等性判斷,如果兩個集合中的元素完全相同,那么==比較的結(jié)果就是True,否則就是False;!=則反之。

如果集合A的所有的元素也是集合B的元素,就稱為A是B的子集,B是A的超集;如果集合A的子級且不等于集合B,那就稱為A是B的真子級。判斷子集和超集的運算符為<和>。示例代碼↓

set1 = {1, 3, 5}
set2 = {1, 2, 3, 4, 5}
set3 = set2
# <運算符表示真子集,<=運算符表示子集
print(set1 < set2, set1 <= set2) # True True
print(set2 < set3, set2 <= set3) # False True
# 通過issubset方法也能進行子集判斷
print(set1.issubset(set2))# True
print(set2.issubset(set2))# True
# issubset方法和<=運算符是等價的
# 反過來可以用issuperset或>運算符進行超集判斷
print(set2.issuperset(set1)) # True
print(set2 > set1)  # True

4、集合的方法

集合屬于可變列類型,可以通過集合類型的方法對集合的元素進行修改,

示例代碼:

# 創(chuàng)建一個空集合
set1 = set()
# 通過add()方法為集合添加元素
set1.add(1)
set1.add(2)
print(set1)  # {1, 2}
# 通過update()給集合添加元素
set1.update({1, 2, 3, 4, 5})
print(set1)  # {1, 2, 3, 4, 5} 元素在集合中已存在,該元素只會出現(xiàn)一次
# 通過discard()方法刪除指定元素
set1.discard(4)
set1.discard(2)
set1.discard(22)  # 集合里面沒有不會出錯
print(set1)  # {1, 3, 5}
# 通過remove()方法刪除指定元素
# set1.remove(6) # KeyError: 6,使用remove()方法刪除指定元素時先做成員運算就不會出錯了
if 6 in set1:
 set1.remove(6)
# pop方法可以從集合中隨機刪除一個元素并返回該元素
print(set1.pop())  # 1 本次剔除的是1
# clear方法可以清空整個集合
set1.clear()
print(type(set1))  # <class 'set'>
print(set1) # set()
# 通過isdisjoint()方法判斷兩個集合有沒有重復的元素,有返回False沒有則返回Ture
set2 = {"Hello", "World"}
set3 = {"Hello", "一碗周"}
set4 = {"Hi", "Python"}
print(set2.isdisjoint(set3))  # False
print(set2.isdisjoint(set4))  # True

5、不可變集合

Python中還有一種不可變類型的集合,名字叫frozenset,于集合的區(qū)別就跟列表和元組的區(qū)別一樣。除了不能添加和刪除元素,frozenset在其他方面跟set基本是一樣的。

示例代碼:

set1 = frozenset({1, 3, 5, 7})
set2 = frozenset(range(1, 6))
print(set1 & set2) # frozenset({1, 3, 5})
print(set1 | set2) # frozenset({1, 2, 3, 4, 5, 7})
print(set1 - set2) # frozenset({7})
print(set1 < set2) # False

到此這篇關(guān)于Python集合的基礎(chǔ)操作的文章就介紹到這了,更多相關(guān)Python集合內(nèi)容請搜索本站以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持本站!

版權(quán)聲明:本站文章來源標注為YINGSOO的內(nèi)容版權(quán)均為本站所有,歡迎引用、轉(zhuǎn)載,請保持原文完整并注明來源及原文鏈接。禁止復制或仿造本網(wǎng)站,禁止在非www.sddonglingsh.com所屬的服務器上建立鏡像,否則將依法追究法律責任。本站部分內(nèi)容來源于網(wǎng)友推薦、互聯(lián)網(wǎng)收集整理而來,僅供學習參考,不代表本站立場,如有內(nèi)容涉嫌侵權(quán),請聯(lián)系alex-e#qq.com處理。

相關(guān)文章

實時開通

自選配置、實時開通

免備案

全球線路精選!

全天候客戶服務

7x24全年不間斷在線

專屬顧問服務

1對1客戶咨詢顧問

在線
客服

在線客服:7*24小時在線

客服
熱線

400-630-3752
7*24小時客服服務熱線

關(guān)注
微信

關(guān)注官方微信
頂部