亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

Python必備技巧之集合Set的使用

 更新時間:2022年03月24日 11:58:54   作者:Mr數(shù)據(jù)楊  
在數(shù)學中,對集合的嚴格定義可能是抽象的且難以掌握。但實際上可以將集合簡單地認為是定義明確的不同對象的集合,通常稱為元素或成員。Python 提供了一個內(nèi)置的集合類型來將對象分組到一個集合中,快跟隨小編一起學習一下吧

在數(shù)學中,對集合的嚴格定義可能是抽象的且難以掌握。但實際上可以將集合簡單地認為是定義明確的不同對象的集合,通常稱為元素或成員。

Python 提供了一個內(nèi)置的集合類型來將對象分組到一個集合中。集合與其他對象類型的區(qū)別在于可以對執(zhí)行的獨特操作。

定義一個集合

集合是無序的,并且元素是唯一的,集合本身可以修改,但集合中包含的元素必須是不可變類型。

構(gòu)建集合的方式

# 構(gòu)建的set數(shù)據(jù)會自動進行去重
x = set(<iter>)
# list方式
>>> x = set(['foo', 'bar', 'baz', 'foo', 'qux'])
>>> x
{'qux', 'foo', 'bar', 'baz'}
# tuple方式
>>> x = set(('foo', 'bar', 'baz', 'foo', 'qux'))
>>> x
{'qux', 'foo', 'bar', 'baz'}
# 字符串方式
>>> s = 'quux'
>>> list(s)
['q', 'u', 'u', 'x']
>>> set(s)
{'x', 'u', 'q'}

集合元素set后自動排序并且元素必須是不可變的。

>>> x = {42, 'foo', (1, 2, 3), 3.14159}
>>> x
{42, 'foo', 3.14159, (1, 2, 3)}
# list和dict不能被set
>>> a = [1, 2, 3]
>>> {a}
Traceback (most recent call last):
  File "<pyshell#70>", line 1, in <module>
    {a}
TypeError: unhashable type: 'list'

>>> d = {'a': 1, 'b': 2}
>>> ublnpf9mb
Traceback (most recent call last):
  File "<pyshell#72>", line 1, in <module>
    ublnpf9mb
TypeError: unhashable type: 'dict'

集合的大小和成員資格

方法 len() 、in 、 not in 的應用。

>>> x = {'foo', 'bar', 'baz'}
>>> len(x)
3
>>> 'bar' in x
True
>>> 'qux' in x
False

集合的9種操作

計算集合并集

# x1.union(x2[, x3 ...])
# x1 | x2 [| x3 ...]
>>> x1 = {'foo', 'bar', 'baz'}
>>> x2 = {'baz', 'qux', 'quux'}
>>> x1 | x2
{'baz', 'quux', 'qux', 'bar', 'foo'}
>>> x1.union(x2)
{'baz', 'quux', 'qux', 'bar', 'foo'}

# 更多的集合并集操作
>>> a = {1, 2, 3, 4}
>>> b = {2, 3, 4, 5}
>>> c = {3, 4, 5, 6}
>>> d = {4, 5, 6, 7}
>>> a.union(b, c, d)
{1, 2, 3, 4, 5, 6, 7}
>>> a | b | c | d
{1, 2, 3, 4, 5, 6, 7}

計算集合交集

# x1.intersection(x2[, x3 ...])
# x1 & x2 [& x3 ...]
>>> x1 = {'foo', 'bar', 'baz'}
>>> x2 = {'baz', 'qux', 'quux'}
>>> x1.intersection(x2)
{'baz'}
>>> x1 & x2
{'baz'}

# 更多的集合交集操作
>>> a = {1, 2, 3, 4}
>>> b = {2, 3, 4, 5}
>>> c = {3, 4, 5, 6}
>>> d = {4, 5, 6, 7}
>>> a.intersection(b, c, d)
{4}
>>> a & b & c & d
{4}

計算集合之間差異

# x1.difference(x2[, x3 ...])
# x1 - x2 [- x3 ...]
>>> x1 = {'foo', 'bar', 'baz'}
>>> x2 = {'baz', 'qux', 'quux'}
>>> x1.difference(x2)
{'foo', 'bar'}
>>> x1 - x2
{'foo', 'bar'}

# 更多的集合差異操作
>>> a = {1, 2, 3, 30, 300}
>>> b = {10, 20, 30, 40}
>>> c = {100, 200, 300, 400}
>>> a.difference(b, c)
{1, 2, 3}
>>> a - b - c
{1, 2, 3}

計算集合間對稱差

# x1.symmetric_difference(x2)
# x1 ^ x2 [^ x3 ...]
>>> x1 = {'foo', 'bar', 'baz'}
>>> x2 = {'baz', 'qux', 'quux'}
>>> x1.symmetric_difference(x2)
{'foo', 'qux', 'quux', 'bar'}
>>> x1 ^ x2
{'foo', 'qux', 'quux', 'bar'}

# 更多的集合對稱差操作
>>> a = {1, 2, 3, 4, 5}
>>> b = {10, 2, 3, 4, 50}
>>> c = {1, 50, 100}
>>> a ^ b ^ c
{100, 5, 10}

計算后集合中是否有包含前集合的元素

# x1.isdisjoint(x2)
>>> x1 = {'foo', 'bar', 'baz'}
>>> x2 = {'baz', 'qux', 'quux'}
>>> x1.isdisjoint(x2)
False
>>> x2 - {'baz'}
{'quux', 'qux'}
>>> x1.isdisjoint(x2 - {'baz'})
True

# x1.isdisjoint(x2)是True,那么x1 & x2是空集
>>> x1 = {1, 3, 5}
>>> x2 = {2, 4, 6}
>>> x1.isdisjoint(x2)
True
>>> x1 & x2
set()

計算一個集合是否是另一個集合的子集

# x1.issubset(x2)
# x1 <= x2
>>> x1 = {'foo', 'bar', 'baz'}
>>> x1.issubset({'foo', 'bar', 'baz', 'qux', 'quux'})
True
>>> x2 = {'baz', 'qux', 'quux'}
>>> x1 <= x2
False
# 一個集合被認為是它自身的一個子集
>>> x = {1, 2, 3, 4, 5}
>>> x.issubset(x)
True
>>> x <= x
True

計算一個集合是否是另一個集合的真子集

# x1 < x2
>>> x1 = {'foo', 'bar'}
>>> x2 = {'foo', 'bar', 'baz'}
>>> x1 < x2
True
>>> x1 = {'foo', 'bar', 'baz'}
>>> x2 = {'foo', 'bar', 'baz'}
>>> x1 < x2
False
# 子集與真子集的判斷
>>> x = {1, 2, 3, 4, 5}
>>> x <= x
True
>>> x < x
False

計算一個集合是否是另一個集合的超集

# x1.issuperset(x2)
# x1 >= x2
>>> x1 = {'foo', 'bar', 'baz'}
>>> x1.issuperset({'foo', 'bar'})
True
>>> x2 = {'baz', 'qux', 'quux'}
>>> x1 >= x2
False

# 集合被認為是本身的一個子集,默認為自身超集
>>> x = {1, 2, 3, 4, 5}
>>> x.issuperset(x)
True
>>> x >= x
True

計算一個集合是否是另一個集合的正確超集

# x1 > x2
>>> x1 = {'foo', 'bar', 'baz'}
>>> x2 = {'foo', 'bar'}
>>> x1 > x2
True
>>> x1 = {'foo', 'bar', 'baz'}
>>> x2 = {'foo', 'bar', 'baz'}
>>> x1 > x2
False
# 集合不是其自身的正確超集
>>> x = {1, 2, 3, 4, 5}
>>> x > x
False

集合的9種修改

盡管集合中包含的元素必須是不可變類型,但集合本身可以修改。

update計算并集

# x1.update(x2[, x3 ...])
# x1 |= x2 [| x3 ...]
>>> x1 = {'foo', 'bar', 'baz'}
>>> x2 = {'foo', 'baz', 'qux'}
>>> x1 |= x2
>>> x1
{'qux', 'foo', 'bar', 'baz'}
>>> x1.update(['corge', 'garply'])
>>> x1
{'qux', 'corge', 'garply', 'foo', 'bar', 'baz'}

intersection_update 計算交集

# x1.intersection_update(x2[, x3 ...])
# x1 &= x2 [& x3 ...]
>>> x1 = {'foo', 'bar', 'baz'}
>>> x2 = {'foo', 'baz', 'qux'}
>>> x1 &= x2
>>> x1
{'foo', 'baz'}
>>> x1.intersection_update(['baz', 'qux'])
>>> x1
{'baz'}

difference_update 按差異修改被處理集合

>>> x1 = {'foo', 'bar', 'baz'}
>>> x2 = {'foo', 'baz', 'qux'}
>>> x1 -= x2
>>> x1
{'bar'}
>>> x1.difference_update(['foo', 'bar', 'qux'])
>>> x1
set()

symmetric_difference_update 按對稱差修改被處理集合

# x1.symmetric_difference_update(x2)
# x1 ^= x2
>>> x1 = {'foo', 'bar', 'baz'}
>>> x2 = {'foo', 'baz', 'qux'}
>>> x1 ^= x2
>>> x1
{'bar', 'qux'}
>>> 
>>> x1.symmetric_difference_update(['qux', 'corge'])
>>> x1
{'bar', 'corge'}

add 元素添加到集合中

>>> x = {'foo', 'bar', 'baz'}
>>> x.add('qux')
>>> x
{'bar', 'baz', 'foo', 'qux'}

remove 集合中移除一個元素

>>> x = {'foo', 'bar', 'baz'}
>>> x.remove('baz')
>>> x
{'bar', 'foo'}
# 如果元素步存在則引發(fā)異常
>>> x.remove('qux')
Traceback (most recent call last):
  File "<pyshell#58>", line 1, in <module>
    x.remove('qux')
KeyError: 'qux'

discard 集合中移除一個元素

>>> x = {'foo', 'bar', 'baz'}
>>> x.discard('baz')
>>> x
{'bar', 'foo'}
>>> x.discard('qux')
>>> x
{'bar', 'foo'}

pop 集合中移除一個隨機元素

>>> x = {'foo', 'bar', 'baz'}
>>> x.pop()
'bar'
>>> x
{'baz', 'foo'}
>>> x.pop()
'baz'
>>> x
{'foo'}
>>> x.pop()
'foo'
>>> x
set()
>>> x.pop()
Traceback (most recent call last):
  File "<pyshell#82>", line 1, in <module>
    x.pop()
KeyError: 'pop from an empty set'

clear 清空集合

>>> x = {'foo', 'bar', 'baz'}
>>> x
{'foo', 'bar', 'baz'}
>>> 
>>> x.clear()
>>> x
set()

被凍結(jié)集合

freezeset 為 Python的內(nèi)置類型,不可變、不可操作。

>>> x = frozenset(['foo', 'bar', 'baz'])
>>> x
frozenset({'foo', 'baz', 'bar'})
>>> len(x)
3
>>> x & {'baz', 'qux', 'quux'}
frozenset({'baz'})

嘗試修改 freezeset 的方法會失敗

>>> x = frozenset(['foo', 'bar', 'baz'])
>>> x.add('qux')
Traceback (most recent call last):
  File "<pyshell#127>", line 1, in <module>
    x.add('qux')
AttributeError: 'frozenset' object has no attribute 'add'
>>> x.pop()
Traceback (most recent call last):
  File "<pyshell#129>", line 1, in <module>
    x.pop()
AttributeError: 'frozenset' object has no attribute 'pop'
>>> x.clear()
Traceback (most recent call last):
  File "<pyshell#131>", line 1, in <module>
    x.clear()
AttributeError: 'frozenset' object has no attribute 'clear'
>>> x
frozenset({'foo', 'bar', 'baz'})

以上就是Python必備技巧之集合Set的使用的詳細內(nèi)容,更多關于Python集合Set的資料請關注腳本之家其它相關文章!

相關文章

  • 通過Python實現(xiàn)一個簡單的html頁面

    通過Python實現(xiàn)一個簡單的html頁面

    這篇文章主要介紹了通過Python寫一個簡單的html頁面,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下
    2020-05-05
  • Python代碼實現(xiàn)找到列表中的奇偶異常項

    Python代碼實現(xiàn)找到列表中的奇偶異常項

    這篇文章主要介紹了Python代碼實現(xiàn)找到列表中的奇偶異常項,文章內(nèi)容主要利用Python代碼實現(xiàn)了從輸入列表中尋找奇偶異常項,需要的朋友可以參考一下
    2021-11-11
  • Python生成隨機數(shù)組的方法小結(jié)

    Python生成隨機數(shù)組的方法小結(jié)

    這篇文章主要介紹了Python生成隨機數(shù)組的方法,結(jié)合實例形式總結(jié)分析了Python使用random模塊生成隨機數(shù)與數(shù)組操作相關技巧,需要的朋友可以參考下
    2017-04-04
  • Python+streamlit實現(xiàn)輕松創(chuàng)建人事系統(tǒng)

    Python+streamlit實現(xiàn)輕松創(chuàng)建人事系統(tǒng)

    streamlit 是 基于 Python 的一個非常強大的 web 構(gòu)建系統(tǒng),通過該類庫,我們可以實現(xiàn)不需要編寫一行前端代碼而構(gòu)建一個完整的 Web 應用。下面我們就來編寫一個簡單的人事系統(tǒng)吧
    2023-02-02
  • 解決Jupyter NoteBook輸出的圖表太小看不清問題

    解決Jupyter NoteBook輸出的圖表太小看不清問題

    這篇文章主要介紹了解決Jupyter NoteBook輸出的圖表太小看不清問題,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-04-04
  • 合并百度影音的離線數(shù)據(jù)( with python 2.3)

    合并百度影音的離線數(shù)據(jù)( with python 2.3)

    這篇文章主要介紹了合并百度影音的離線數(shù)據(jù)( with python 2.3)的相關資料
    2015-08-08
  • Flask-Sqlalchemy的基本使用詳解

    Flask-Sqlalchemy的基本使用詳解

    本文主要介紹了Flask-Sqlalchemy的基本使用詳解,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2022-06-06
  • Python matplotlib 動畫繪制詳情

    Python matplotlib 動畫繪制詳情

    這篇文章主要介紹了Python matplotlib 動畫繪制,文章圍繞主題展開詳細的內(nèi)容介紹,具有一定的參考價值,感興趣的小伙伴可以參考一下
    2022-09-09
  • 淺析pandas隨機排列與隨機抽樣

    淺析pandas隨機排列與隨機抽樣

    這篇文章主要介紹了pandas隨機排列與隨機抽樣的相關資料,幫助大家更好的利用pandas進行數(shù)據(jù)分析,感興趣的朋友可以了解下
    2021-01-01
  • Python線程協(xié)作threading.Condition實現(xiàn)過程解析

    Python線程協(xié)作threading.Condition實現(xiàn)過程解析

    這篇文章主要介紹了Python線程協(xié)作threading.Condition實現(xiàn)過程解析,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下
    2020-03-03

最新評論