Python集合set的交集和并集操作方法
前言:
集合這種數(shù)據(jù)類型和我們數(shù)學(xué)中所學(xué)的集合很是相似,數(shù)學(xué)中堆積和的操作也有交集,并集和差集操作,python集合也是一樣。
一、交集操作
1.使用intersection()求交集
可變集合和不可變集合求交集的時候,用什么集合調(diào)用交集方法,返回的結(jié)果就是什么類型的集合。
set7 = {'name', 18, 'python2', 'abc'} set8 = frozenset({'name', 19, 'python3', 'abc'}) res = set7.intersection(set8) ?# {'abc', 'name'} <class 'set'> print(res, type(res)) res = set8.intersection(set7) ?# frozenset({'abc', 'name'}) <class 'frozenset'> print(res, type(res))
返回結(jié)果:
{'abc', 'name'} <class 'set'>
frozenset({'abc', 'name'}) <class 'frozenset'>
2. 使用位運算&符求交集
set5 = {'name', 18, 'python2', 'abc'} set6 = {'name', 19, 'python3', 'abc'} set7 = {'name', 18, 'python2', 'abc'} set8 = frozenset({'name', 19, 'python3', 'abc'}) res = set5 & set6 print(res, type(res)) res = set7 & set8 print(res, type(res)) res = set8 & set7 ?# 誰在前,返回結(jié)果就和誰是同樣類型的集合 print(res, type(res))
返回結(jié)果:
{'abc', 'name'} <class 'set'>
{'abc', 'name'} <class 'set'>
frozenset({'abc', 'name'}) <class 'frozenset'>
3.intersection_update()方法
使用此方法計算出交集之后會把結(jié)果賦值給原有的集合,屬于一種更改,所以不適用于不可變集合
set7 = {'name', 18, 'python2', 'abc'} set8 = frozenset({'name', 19, 'python3', 'abc'}) res = set7.intersection_update(set8) ?# 沒有返回值 print(set7, type(set7)) ?# 沒有返回值,直接打印被賦值集合 res = set8.intersection_update(set7) ?# 不可變集合沒有intersection_update方法 print(res, type(res))
返回結(jié)果:
{'abc', 'name'} <class 'set'>
AttributeError: 'frozenset' object has no attribute 'intersection_update'
4.使用intersection()方法
使用此方法求集合和其他數(shù)據(jù)類型的交集時intersection()
會把其他數(shù)據(jù)類型直接轉(zhuǎn)為集合。
str1 = 'python' list1 = [1, 2, 3, 18] tup1 = (1, 2, 3, 18) dict1 = {'name': 'Tom', 'age': 18, 'love': 'python'} set10 = {'name', 18, 'python', 'abc', 'p'} print(set10.intersection(str1)) ? # 返回:{'p'}而不是{'python'},因為str1轉(zhuǎn)成集合為:{'y', 't', 'p', 'o', 'n', 'h'} ? print(set10.intersection(list1)) print(set10.intersection(tup1)) print(set10.intersection(dict1))
返回結(jié)果:
{'p'}
{18}
{18}
{'name'}
二、并集操作
1.使用union()求并集
set5 = {'name', 18, 'python2', 'abc'} set6 = {'name', 19, 'python3', 'abc'} res = set5.union(set6) print(res, type(res))
返回結(jié)果:
{'python2', 'abc', 18, 19, 'python3', 'name'} <class 'set'>
2.使用邏輯或 | 求并集
set5 = {'name', 18, 'python2', 'abc'} set6 = {'name', 19, 'python3', 'abc'} res = set5 | set6 print(res, type(res))
返回結(jié)果:
{'abc', 'python2', 'name', 'python3', 18, 19} <class 'set'>
3.使用update()求并集,只能作用域可變集合
set5 = {'name', 18, 'python2', 'abc'} set6 = {'name', 19, 'python3', 'abc'} res = set5.update(set6) ?# 有黃色波浪線表示這個函數(shù)沒有返回值 print(set5, type(set5))
返回結(jié)果:
{'python2', 'python3', 18, 'abc', 19, 'name'} <class 'set'>
到此這篇關(guān)于Python集合set的交集和并集操作方法小的文章就介紹到這了,更多相關(guān)Python集合set內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Python數(shù)據(jù)預(yù)處理常用的5個技巧
大家好,本篇文章主要講的是Python數(shù)據(jù)預(yù)處理常用的5個技巧,感興趣的同學(xué)趕快來看一看吧,對你有幫助的話記得收藏一下2022-02-02Python+Qt身體特征識別人數(shù)統(tǒng)計源碼窗體程序(使用步驟)
這篇文章主要介紹了Python+Qt身體特征識別人數(shù)統(tǒng)計源碼窗體程序(使用步驟),本文通過示例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2022-12-12python使用for...else跳出雙層嵌套循環(huán)的方法實例
這篇文章主要給大家介紹了關(guān)于python使用for...else跳出雙層嵌套循環(huán)的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家學(xué)習(xí)或者使用python具有一定的參考學(xué)習(xí)價值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧2020-05-05Win10環(huán)境中如何實現(xiàn)python2和python3并存
這篇文章主要介紹了Win10環(huán)境中如何實現(xiàn)python2和python3并存,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2020-07-07python DataFrame轉(zhuǎn)dict字典過程詳解
這篇文章主要介紹了python DataFrame轉(zhuǎn)dict字典過程詳解,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2019-12-12python實現(xiàn)內(nèi)存監(jiān)控系統(tǒng)
這篇文章主要為大家詳細(xì)介紹了python實現(xiàn)內(nèi)存監(jiān)控系統(tǒng),通過系統(tǒng)命令或操作系統(tǒng)文件獲取到內(nèi)存信息,具有一定的參考價值,感興趣的小伙伴們可以參考一下2018-06-06Python從使用線程到使用async/await的深入講解
Python在3.5版本中引入了關(guān)于協(xié)程的語法糖async和await,所以下面這篇文章主要給大家介紹了關(guān)于Python從使用線程到使用async/await的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),需要的朋友可以參考下2018-09-09