前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Python求交集和并集方法和示例操作 【详解】

Python求交集和并集方法和示例操作 【详解】

原创
作者头像
python自学网
发布2021-12-04 17:51:13
6.5K0
发布2021-12-04 17:51:13
举报

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

求交集和并集方法和示例操作
求交集和并集方法和示例操作

一、交集操作

## 1.使用intersection()求交集:

可变集合和不可变集合求交集的时候,用什么集合调用交集方法,返回的结果就是什么类型的集合。

代码语言:python
复制
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))

返回结果:

代码语言:python
复制
{'abc', 'name'} <class 'set'>
frozenset({'abc', 'name'}) <class 'frozenset'>

## 2. 使用位运算&符求交集

代码语言:python
复制
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))

返回结果:

代码语言:python
复制
{'abc', 'name'} <class 'set'>
{'abc', 'name'} <class 'set'>
frozenset({'abc', 'name'}) <class 'frozenset'>

## 3.intersection_update()方法

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

代码语言:python
复制
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))

返回结果:

代码语言:python
复制
{'abc', 'name'} <class 'set'>
AttributeError: 'frozenset' object has no attribute 'intersection_update'

## 4.使用intersection()方法

使用此方法求集合和其他数据类型的交集时intersection()会把其他数据类型直接转为集合。

代码语言:python
复制
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))

返回结果:

代码语言:python
复制
{'p'}
{18}
{18}
{'name'}

二、并集操作

## 1.使用union()求并集

代码语言:python
复制
set5 = {'name', 18, 'python2', 'abc'}
set6 = {'name', 19, 'python3', 'abc'}
res =?set5.union(set6)rint(res,?type(res))

返回结果:

代码语言:python
复制
{'python2', 'abc', 18, 19, 'python3', 'name'} <class 'set'>

## 2.使用逻辑或 | 求并集

代码语言:python
复制
set5 = {'name', 18, 'python2', 'abc'}
set6 = {'name', 19, 'python3', 'abc'}
res =?set5 |?set6
print(res,?type(res))

返回结果:

代码语言:python
复制
{'abc', 'python2', 'name', 'python3', 18, 19} <class 'set'>

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

代码语言:python
复制
set5 = {'name', 18, 'python2', 'abc'}
set6 = {'name', 19, 'python3', 'abc'}
res =?set5.update(set6) # 有黄色波浪线表示这个函数没有返回值print(set5,?type(set5))

返回结果:

代码语言:python
复制
{'python2', 'python3', 18, 'abc', 19, 'name'} <class 'set'>

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

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 一、交集操作
    • ## 1.使用intersection()求交集:
      • ## 2. 使用位运算&符求交集
        • ## 3.intersection_update()方法
          • ## 4.使用intersection()方法
          • 二、并集操作
            • ## 1.使用union()求并集
              • ## 2.使用逻辑或 | 求并集
                • ## 3.使用update()求并集,只能作用域可变集合
                领券
                问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档
                http://www.vxiaotou.com