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

Python中關(guān)于set的基本用法

 更新時間:2023年04月25日 09:01:35   作者:YKenan  
這篇文章主要介紹了Python中關(guān)于set的基本用法,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教

1. set 的基本內(nèi)容

1.基本特點(diǎn)

  • (1) 無序性
  • (2) 確定性
  • (3) 不重復(fù)性

2.set() 實(shí)質(zhì)

內(nèi)部進(jìn)行 可迭代性的 for 循環(huán)

例子:

2. set 的基本方法

2.1 set 的普通基本方法

2.1.1 增

add(self, *args, **kwargs)
copy(self, *args, **kwargs)
# 1. 增

# Add an element to a set. This has no effect if the element is already present.
s = {1, 12, 32, "漣漪", "hello"}
s.add("good")
s.add(32)
print(s)

# Add an element to a set. This has no effect if the element is already present.
s = {1, 12, 32, "漣漪", "hello"}
c = s.copy()
print(c)

結(jié)果:

2.1.1 刪

clear(self, *args, **kwargs)
pop(self, *args, **kwargs)
remove(self, *args, **kwargs)
discard(self, *args, **kwargs)
# 2. 刪

# Remove all elements from this set.
s = {1, 12, 32, "漣漪", "hello"}
s.clear()
print(s)

# Remove and return an arbitrary set element. Raises KeyError if the set is empty.
s = {1, 12, 32, "漣漪", "hello"}
s.pop()
print(s)

# Remove an element from a set; it must be a member. If the element is not a member, raise a KeyError.
s = {1, 12, 32, "漣漪", "hello"}
s.remove(1)
# s.remove("good")
print(s)

# Remove an element from a set if it is a member. If the element is not a member, do nothing.
s = {1, 12, 32, "漣漪", "hello"}
s.discard(1)
s.discard("good")
print(s)

結(jié)果:

pop() 是隨機(jī)刪除。

remove() 和 discard() 指定刪除,但是指定不存在的元素時,remove() 會報(bào)錯,而 discard() 不會報(bào)錯

2.2 set 的邏輯基本方法

2.2.1 set 交集運(yùn)算

# set 交集運(yùn)算
x1 = ["a", "b", "c", "d", "e"]
x2 = ["c", "d", "e", "f", "g"]
x3 = []
for x in x1:
    if x in x2:
        x3.append(x)
print(x3)

s_x1 = set(x1)
s_x2 = set(x2)
inter = s_x1.intersection(s_x2)
print(inter)
# 交集符號運(yùn)算
print(s_x1 & s_x2)
# update
s_x1.intersection_update(s_x2)
print(s_x1)

結(jié)果:

2.2.2 set 并集運(yùn)算

# set 并集運(yùn)算
x1 = ["a", "b", "c", "d", "e"]
x2 = ["c", "d", "e", "f", "g"]
s_x1 = set(x1)
s_x2 = set(x2)
uni = s_x1.union(s_x2)
print(uni)
# 并集符號運(yùn)算
print(s_x1 | s_x2)
# update
s_x1.update(s_x2)
print(s_x1)

結(jié)果:

2.2.3 set 差集運(yùn)算

# set 差集運(yùn)算
x1 = ["a", "b", "c", "d", "e"]
x2 = ["c", "d", "e", "f", "g"]
s_x1 = set(x1)
s_x2 = set(x2)
dif_x1 = s_x1.difference(s_x2)
print(dif_x1)
dif_x2 = s_x2.difference(s_x1)
print(dif_x2)
# 差集符號運(yùn)算
print(s_x1 - s_x2)
print(s_x2 - s_x1)
# update
s_x1.difference_update(s_x2)
print(s_x1)
s_x2.difference_update(s_x1)
print(s_x2)

結(jié)果:

2.2.4 set 對稱差集運(yùn)算

# set 對稱差集運(yùn)算滿足交換律:A△B = B△A
s_x1 = set(x1)
s_x2 = set(x2)
sym = s_x1.symmetric_difference(s_x2)
print(sym)
# 對稱差集符號運(yùn)算
print(s_x1 ^ s_x2)
print(s_x1 - s_x2 | s_x2 - s_x1)
print((s_x1 | s_x2) - (s_x2 & s_x1))
# update
s_x1.symmetric_difference_update(s_x2)
print(s_x1)

結(jié)果:

2.2.5 set 邏輯判斷運(yùn)算

# 判斷
# Return True if two sets have a null intersection.
x1 = {"a", "b", "c"}
x2 = {"e", "f", "g"}
inter = x1.isdisjoint(x2)
print(inter)

# Report whether another set contains this set.
x1 = {"a", "b", "c"}
x2 = {"a", "b", "c", "e", "f", "g"}
inter = x1.issubset(x2)
print(inter)

# Report whether this set contains another set.
x1 = {"a", "b", "c", "e", "f", "g"}
x2 = {"a", "b", "c"}
inter = x1.issuperset(x2)
print(inter)

結(jié)果:

總結(jié)

以上為個人經(jīng)驗(yàn),希望能給大家一個參考,也希望大家多多支持腳本之家。

相關(guān)文章

  • Python字典取鍵、值對的方法步驟

    Python字典取鍵、值對的方法步驟

    這篇文章主要介紹了Python字典取鍵、值對的方法步驟,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-09-09
  • Python中的魔法方法深入理解

    Python中的魔法方法深入理解

    這篇文章主要介紹了Python中的魔法方法深入理解,本文通過分析WEB框架Flask的源碼來分析Python中的魔法方法,需要的朋友可以參考下
    2014-07-07
  • Python爬蟲之自動爬取某車之家各車銷售數(shù)據(jù)

    Python爬蟲之自動爬取某車之家各車銷售數(shù)據(jù)

    應(yīng)朋友要求,幫忙采集某車之家的一些汽車品牌的銷售數(shù)據(jù),包含購車時間、車型、經(jīng)銷商、裸車價等一類信息. 今天我們就簡單演示一下采集過程,大家可以根據(jù)自己的興趣進(jìn)行拓展.比如采集自己喜歡的品牌汽車數(shù)據(jù)進(jìn)行統(tǒng)計(jì)分析等等,需要的朋友可以參考下
    2021-06-06
  • 解決pytorch中的kl divergence計(jì)算問題

    解決pytorch中的kl divergence計(jì)算問題

    這篇文章主要介紹了解決pytorch中的kl divergence計(jì)算問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-05-05
  • Python面向?qū)ο笾惡蛯ο髮?shí)例詳解

    Python面向?qū)ο笾惡蛯ο髮?shí)例詳解

    這篇文章主要介紹了Python面向?qū)ο笾惡蛯ο?結(jié)合實(shí)例形式詳細(xì)分析了Python面向?qū)ο笙嚓P(guān)的繼承、多態(tài)、類及對象等概念、原理、操作技巧與注意事項(xiàng),需要的朋友可以參考下
    2018-12-12
  • 對Pandas MultiIndex(多重索引)詳解

    對Pandas MultiIndex(多重索引)詳解

    今天小編就為大家分享一篇對Pandas MultiIndex(多重索引)詳解,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2018-11-11
  • 詳解python 破解網(wǎng)站反爬蟲的兩種簡單方法

    詳解python 破解網(wǎng)站反爬蟲的兩種簡單方法

    這篇文章主要介紹了詳解python 破解網(wǎng)站反爬蟲的兩種簡單方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-02-02
  • python中的elasticsearch_dsl查詢語句轉(zhuǎn)換成es查詢語句詳解

    python中的elasticsearch_dsl查詢語句轉(zhuǎn)換成es查詢語句詳解

    這篇文章主要介紹了python中的elasticsearch_dsl查詢語句轉(zhuǎn)換成es查詢語句詳解,ElasticSearch在實(shí)際生產(chǎn)里通常和LogStash,Kibana,F(xiàn)ileBeat一起構(gòu)成Elastic?Stack來使用,它是這些組件里面最核心的一個,需要的朋友可以參考下
    2023-07-07
  • Python Pillow(PIL)庫的用法詳解

    Python Pillow(PIL)庫的用法詳解

    這篇文章主要介紹了Python Pillow(PIL)庫的用法介紹,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2020-09-09
  • python字符串對其居中顯示的方法

    python字符串對其居中顯示的方法

    這篇文章主要介紹了python字符串對其居中顯示的方法,涉及Python打印輸出顯示的相關(guān)技巧,具有一定參考借鑒價值,需要的朋友可以參考下
    2015-07-07

最新評論