• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

关于python:几大Python集合的交集和并集操作方法和示例

python 搞代码 4年前 (2022-02-20) 31次浏览 已收录 0个评论

汇合这种数据类型和咱们数学中所学的汇合很是类似,数学中沉积和的操作也有交加,并集和差集操作,python汇合也是一样。

一、交加操作

1.应用intersection()求交加:

可变汇合和不可变汇合求交加的时候,用什么汇合调用交加办法,返回的后果就是什么类型的汇合。

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))

返回后果:

{'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  # 谁在前,返回后果就和谁是同样类型的汇合
print(res, type(res))

返回后果:

{'abc', 'name'} <class 'set'>
{'abc', 'name'} <class 'set'>
frozenset({'abc', 'name'}) <class 'frozenset'>

3.intersection_update()办法

应用此办法计算出交加之后会把后果赋值给原有的汇合,属于一种更改,所以不适用于不可变汇合

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))

返回后果:

{'abc', 'name'} <class 'set'>
AttributeError: 'frozenset' object has no attribute 'intersection_update'

4.应用intersection()办法

应用此办法求汇合和其余数据类型的交加时intersection()会把其余数据类型间接转为汇合。

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转成汇合为:{'y', 't', 'p', 'o', 'n', 'h'}
print(set10.intersection(list1))
print(set10.intersection(tup1))
print(set10.intersection(dict1))

返回后果:

{'p'}
{18}
{18}
{'name'}

二、并集操作

1.应用union()求并集

set5 = {'name', 18, 'python2', 'abc'}
set6 = {'name', 19, 'python3', 'abc'}
res = set5.union(set6)
print(res, type(res))
返回后果:
{'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))
返回后果:
{'abc', 'python2', 'name', 'python3', 18, 19} <class 'set'>

3.应用update()求并集,只能作用域可变汇合

set5 = {'name', 18, 'python2', 'abc'}
set6 = {'name', 19, 'python3', 'abc'}
res = set5.update(set6)  # 有黄色波浪线示意这个函数没有返回值print(set5, type(set5))
返回后果:
{'python2', 'python3', 18, 'abc', 19, 'name'} <class 'set'>

下面讲了Python汇合的交加和并集操作以及用一些实例演示了一番,可能第一遍学习的时候不是特地了解,没关系一遍就行就多来几遍,或者看Python自学网视频教程会更好一些,文字教程可能没方法把所有的知识点都概括进来。

文章起源:www.wakey.com.cn/document-set-flex.html


搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:关于python:几大Python集合的交集和并集操作方法和示例

喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址