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

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

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

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

1.基本特點

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

2.set() 實質(zhì)

內(nèi)部進行 可迭代性的 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() 是隨機刪除。

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

2.2 set 的邏輯基本方法

2.2.1 set 交集運算

# set 交集運算
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)
# 交集符號運算
print(s_x1 & s_x2)
# update
s_x1.intersection_update(s_x2)
print(s_x1)

結(jié)果:

2.2.2 set 并集運算

# set 并集運算
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)
# 并集符號運算
print(s_x1 | s_x2)
# update
s_x1.update(s_x2)
print(s_x1)

結(jié)果:

2.2.3 set 差集運算

# set 差集運算
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)
# 差集符號運算
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 對稱差集運算

# set 對稱差集運算滿足交換律:A△B = B△A
s_x1 = set(x1)
s_x2 = set(x2)
sym = s_x1.symmetric_difference(s_x2)
print(sym)
# 對稱差集符號運算
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 邏輯判斷運算

# 判斷
# 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)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。

相關(guān)文章

  • Python函數(shù)生成器原理及使用詳解

    Python函數(shù)生成器原理及使用詳解

    這篇文章主要介紹了Python函數(shù)生成器原理及使用詳解,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下
    2020-03-03
  • Python深入學習之裝飾器

    Python深入學習之裝飾器

    這篇文章主要介紹了Python深入學習之裝飾器,裝飾器(decorator)是一種高級Python語法,本文全面介紹了Python中的裝飾器,需要的朋友可以參考下
    2014-08-08
  • Django一小時寫出賬號密碼管理系統(tǒng)

    Django一小時寫出賬號密碼管理系統(tǒng)

    這篇文章主要介紹了Django一小時寫出賬號密碼管理系統(tǒng),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2021-04-04
  • python csv實時一條一條插入且表頭不重復問題

    python csv實時一條一條插入且表頭不重復問題

    這篇文章主要介紹了python csv實時一條一條插入且表頭不重復問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-05-05
  • python實現(xiàn)記事本功能

    python實現(xiàn)記事本功能

    這篇文章主要為大家詳細介紹了python實現(xiàn)記事本功能,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2022-01-01
  • python讓列表倒序輸出的實例

    python讓列表倒序輸出的實例

    今天小編就為大家分享一篇python讓列表倒序輸出的實例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2018-06-06
  • python numpy和list查詢其中某個數(shù)的個數(shù)及定位方法

    python numpy和list查詢其中某個數(shù)的個數(shù)及定位方法

    今天小編就為大家分享一篇python numpy和list查詢其中某個數(shù)的個數(shù)及定位方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2018-06-06
  • 基于Python實現(xiàn)植物大戰(zhàn)僵尸游戲的示例代碼

    基于Python實現(xiàn)植物大戰(zhàn)僵尸游戲的示例代碼

    植物大戰(zhàn)僵尸是一款經(jīng)典的塔防類游戲,玩家通過種植各種植物來抵御僵尸的攻擊,本文將詳細介紹如何使用Python和Pygame庫來實現(xiàn)一個簡單的植物大戰(zhàn)僵尸游戲,文中通過代碼示例講解的非常詳細,感興趣的小伙伴跟著小編一起來看看吧
    2024-10-10
  • python機器學習庫常用匯總

    python機器學習庫常用匯總

    這篇文章主要為大家匯總了常用python機器學習庫,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-11-11
  • Python設計模式之抽象工廠模式原理與用法詳解

    Python設計模式之抽象工廠模式原理與用法詳解

    這篇文章主要介紹了Python設計模式之抽象工廠模式,簡單講述了抽象工廠模式的概念、原理并結(jié)合實例形式分析了Python實現(xiàn)與使用抽象工廠模式的相關(guān)操作技巧,需要的朋友可以參考下
    2019-01-01

最新評論

民丰县| 博乐市| 永嘉县| 淮安市| 巢湖市| 宜春市| 铜山县| 皮山县| 铅山县| 梅河口市| 临沂市| 屯昌县| 会同县| 古浪县| 滁州市| 武定县| 灵山县| 扎囊县| 龙胜| 淮北市| 奉节县| 灵武市| 怀化市| 惠州市| 北京市| 清流县| 高清| 安西县| 建始县| 阿坝县| 东平县| 井陉县| 思南县| 南昌县| 拉萨市| 南部县| 辽宁省| 崇左市| 丰顺县| 内江市| 弥勒县|