python集合的运算、不使用有的运算符_无序的集合:Python中的数学集合运算
全文共8958字,预计学习时长23分钟
图源:unsplash
set是Python中无序的集合,它可以用于计算标准数学运算,例如交集、并集、差集和对称差集,Other集合(例如列表、元组和字典)不支持集合操作,Dict视图对象类似于集合,可以进行集合操作。本文将详细探讨set对象支持的数学运算。
先来看一下Set对象支持的数学运算:
· union()
· update()
· intersection()
· intersection_update()
· difference()
· difference_update()
· symmetric_difference()
· symmetric_difference_update()
· isdisjoint()
· issubset()
· issuperset()
Set运算操作可以通过两种方式完成:使用方法或运算符。
‘union()’
返回一个新集合,其中包含该set和other集合中的元素,使用union()或“|“算子。
语法:
union(*others)
set | other | ...
示例1:找到两个集合的并集—A和B
返回一个包含集合A和集合B中的元素的新集合。但元素不会重复,集合中的所有元素都是唯一的。
A={1,2,3,4,5}
B={2,4,6,8}
print (A.union(B))#Output:{1, 2, 3, 4, 5, 6, 8}
print (A|B)#Output:{1, 2, 3, 4, 5, 6, 8}
示例2:查找两个以上集合的并集
A={1,2,3,4,5}
B={2,4,6,8,10}
C={1,3,5,7,9}
print (A|B|C)#Output:{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
print (A.union(B,C))#Output:{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
union()方法和|之间的区别:
· union():接受任何可迭代的参数
· |运算符:仅接受set作为参数,否则将引发TypeError。
示例3:在union()方法中将iterable用作参数
A={1,2,3,4,5}
#iterable is given as list
print (A.union([2,4,6]))#Output:{1, 2, 3, 4, 5, 6}
#iterable is given as tuple
print (A.union((2,4,6)))#Output:{1, 2, 3, 4, 5, 6}
#iterable is given as range object
print (A.union(range(5,10)))#Output:{1, 2, 3, 4, 5, 6, 7, 8, 9}
#iterable is given as a dictionary
print (A.union({'a':6,'b':7}))#Output:{1, 2, 3, 4, 5, 'b', 'a'}
示例4:为|提供参数iterable
A={1,2,3,4,5}
B=[1,2,3]
print (A|B)
#Output:TypeError: unsupported operand type(s) for |: 'set' and 'list'
‘update()’
更新集合,并从other集合中添加元素,元素不会重复,集合中的所有元素都是唯一的。通过使用update() 或使用|=运算符来执行,返回类型为None,将修改原始集本身。
语法:
update(*others)
set |= other | ...
示例1:在A和B两个集合之间调用update()
通过添加两个集合中的元素来更新集合A。
#update()
A={1,2,3,4,5}
B={4,5,6,7,8}
print (A.update(B)) #Output: None
print (A) #Output: {1, 2, 3, 4, 5, 6, 7, 8}A={1,2,3,4,5}
B={4,5,6,7,8}
A|=B
print (A) #Output: {1, 2, 3, 4, 5, 6, 7, 8}
示例2:在两个以上集合之间调用update()
#update()
A={1,2,3}
B={3,4,5}
C={5,6,7}
print (A.update(B,C)) #Output: None
print (A) #Output: {1, 2, 3, 4, 5, 6, 7}
A={1,2,3}
B={3,4,5}
C={5,6,7}
A|=B|C
print (A) #Output: {1, 2, 3, 4, 5, 6, 7}
update() 方法和|=运算符之间的区别:
· update() :接受任何可迭代的参数。
· =运算符:仅接受set作为参数,否则将引发TypeError。
示例3:在update() 方法中将iterable用作参数
A={1,2,3}
#iterable is given as list
print (A.update([2,3,4]))#Output:None
print (A)#Output:{1,2,3,4}
#iterable is given as tuple
A={1,2,3}
A.update((2,3,4))
print (A)#Output:{1,2,3,4}
#iterable is given as range object
A={1,2,3}
A.update(range(2,5))
print (A)#Output:{1,2,3,4}
#iterable is given as a dictionary
A={1,2,3}
A.update({2:'a',3:'b'})
print (A) #Output:{1, 2, 3}
示例4:为|=运算符提供参数iterable:
#iterable is given as tuple
A={1,2,3}
B=(3,4)
A|=B
#Output:TypeError: unsupported operand type(s) for |=: 'set' and 'tuple'
‘intersection()’
返回一个具有该集合和other集合共同元素的新集合,通过intersection()或使用&运算符来执行。
语法:
intersection(*others)
set & other & ...
示例1:找到两个集合的交集—A和B
返回一个新集合,其中包含集合A和集合B中的共同元素。
A={1,2,3,4,5}
B={2,4,6,8}
#intersection is performed by intersection() method or & operator
print (A.intersection(B))#Output:{2,4}
print (A&B)#Output:{2,4}
示例2:找到两个以上的交集
A={1,2,3,4,5}
B={2,4,6,8,10}
C={2,4}
print (A&B&C)#Output:{2,4}
print (A.intersection(B,C))#Output:{2,4}
intersection()方法和&运算符之间的区别:
· intersection():接受任何可迭代的参数。
· &运算符:仅接受set参数,否则将引发TypeError。
示例3:在intersection()方法中将iterable用作参数
A={1,2,3,4,5}
#iterable is given as list
print (A.intersection([1,4,6]))#Output:{1,4}
#iterable is given as tuple
print (A.intersection((2,4,6)))#Output:{2,4}
#iterable is given as range object
print (A.intersection(range(5,10)))#Output:{5}
#iterable is given as a dictionary
print (A.intersection({1:'a','b':7}))#Output:{1}
示例4:为&运算符提供参数iterable
A={1,2,3,4,5}
B=[1,2,3]
print (A&B)
#Output:TypeError: unsupported operand type(s) for &: 'set' and 'list'
‘intersection_update()’
更新集合,只保留集合和other中共同的元素。可以通过使用 intersection_update()或使用&=运算符来执行,返回类型为None,将修改原始集本身。
语法:
intersection_update(*others)
set &= other & …
示例1:找到两个集合A和B之间的 intersection_update()
通过仅保留在两个集合中找到的元素来更新集合A。
#intersection_update()
A={1,2,3,4,5}
B={4,5,6,7,8}
print (A.intersection_update(B)) #Output: None
print (A) #Output: {4,5}A={1,2,3,4,5}
B={4,5,6,7,8}
A&=B
print (A) #Output: {4,5}
‘difference()’
返回一个去除other中元素之后的新集合,通过difference() 或使用-运算符来执行。
语法:
difference(*others)
set - other - ...
示例1:找出A和B两组之间的差异
返回一个新集合,其中包含在集合A而不在集合B中的元素。
A={1,2,3,4,5}
B={2,4,6,8}
print (A.difference(B))#Output:{1,3,5}
print (A-B)#Output:{1,3,5}
示例2:找出两个以上集合之间的差异
A={1,2,3,4,5}
B={2,4,6,8,10}
C={2,3}
print (A-B-C)#Output:{1,5}
print (A.difference(B,C))#Output:{1,5}
difference()方法和-运算符之间的区别:
· difference():接受任何可迭代的参数
· -运算符:仅接受set作为参数。否则将引发TypeError。
示例3:在difference()方法中将iterable作为参数
A={1,2,3,4,5}
#iterable is given as list
print (A.difference([1,2,3]))#Output:{4,5}
#iterable is given as tuple
print (A.difference((1,2,3)))#Output:{4,5}
#iterable is given as range object
print (A.difference(range(1,4)))#Output:{4,5}
#iterable is given as a dictionary
print (A.difference({1:'a',2:'b',3:'c'}))#Output:{4,5}
示例4:为-运算符提供参数iterable
A={1,2,3,4,5}
B=[1,2,3]
print (A-B)
#Output:TypeError: unsupported operand type(s) for -: 'set' and 'list'
‘difference_update()’
从other集合中删除该集合中的元素,通过使用-= 运算符或使用difference_update() 方法来执行,返回类型为None,将修改原始集本身。
句法:
difference_update(*others)
set -= other | ...
示例1:找出A和B两组之间的差异
返回一个新集合,其中包含在集合A而不在集合B中的元素。
A={1,2,3,4,5}
B={2,4,6,8}
print (A.difference(B))#Output:{1,3,5}
print (A-B)#Output:{1,3,5}
示例2:找出两个以上集合之间的差异
A={1,2,3,4,5}
B={2,4,6,8,10}
C={2,3}
print (A-B-C)#Output:{1,5}
print (A.difference(B,C))#Output:{1,5}
difference()方法和-运算符之间的区别:
· difference():接受任何可迭代的参数
· -运算符:仅接受set作为参数。否则将引发TypeError。
示例3:在difference()方法中将iterable作为参数
A={1,2,3,4,5}
#iterable is given as list
print (A.difference([1,2,3]))#Output:{4,5}
#iterable is given as tuple
print (A.difference((1,2,3)))#Output:{4,5}
#iterable is given as range object
print (A.difference(range(1,4)))#Output:{4,5}
#iterable is given as a dictionary
print (A.difference({1:'a',2:'b',3:'c'}))#Output:{4,5}
示例4:为-运算符提供参数iterable
A={1,2,3,4,5}
B=[1,2,3]
print (A-B)
#Output:TypeError: unsupported operand type(s) for -: 'set' and 'list'
‘difference_update()’
从other集合中删除该集合中的元素,通过使用-= 运算符或使用difference_update() 方法来执行,返回类型为None,将修改原始集本身。
句法:
difference_update(*others)
set -= other | ...
示例1:找到两个集合A和B之间的difference_update()
通过删除集合A和集合B中都存在的元素来更新集合A。
A={1,2,3,4,5}
B={2,4,6}
#Return type is None.
print (A.difference_update(B))#Output:None
#It will update the original set
print (A) #Output: {1,3,5}
# difference_update by using -= operator
A-=(B)
print (A) #Output: {1,3,5}
示例2:查找两个以上集合之间的difference_update
#difference_update() will modify the original set.
A={1,2,3}
B={1}
C={2}
#Return type is None.
print (A.difference_update(B,C))#Output:None
#It will update the original set
print (A) #Output: {3}
# difference_update by using -= operator
A={1,2,3}
B={1}
C={2}
A-=B|C
print (A) #Output: {3}
difference_update()方法与-=运算符的区别:
· difference_update():接受任何可迭代的参数
· -=运算符:仅接受set参数,否则将引发TypeError。
示例3:在difference_update()方法中将iterable作为参数
#iterable is given as list
A={1,2,3}
B=[1]
print (A.difference_update(B))#Output:None
print (A)#Output:{2,3}
示例4:为-=运算符提供参数iterable
A={1,2,3}
B=[1]
A-=B
print (A)
#Output: TypeError: unsupported operand type(s) for -=: 'set' and 'list'
‘symmetric_difference()’
返回一个新集合,该集合中的元素属于集合或other,但不包含两个集合共有的元素。通过symmetric_difference()或使用^运算符来执行。
语法:
symmetric_difference(other)
set ^ other
示例1:找到A和B两组之间的对称差
返回一个新集合,其中包含来自集合A和集合B的元素,但不包含两个集合中共同的元素。
A={1,2}
B={2,3}
print (A.symmetric_difference(B))#Output:{1,3}
print (A^B)#Output:{1,3}
示例2:对称差集仅适用于2个集合
多个集合不支持symmetric_difference()方法,如果给出两个以上的集合,则会引发TypeError。
A={1,2}
B={2,3,5}
C={3,4}
print (A.symmetric_difference(B,C))#Output:TypeError:symmetric_difference() takes exactly one argument (2 given)
但是我们可以使用^找到多个集合的对称差集
A={1,2}
B={2,3,5}
C={3,4}
print (A^B^C)#Output:{1,4,5}
symmetric_difference()方法和^运算符之间的区别:
· symmetric_difference():接受任何可迭代的参数,但此方法不允许使用多个集合。
· ^运算符:它将仅接受set作为参数。否则,将引发TypeError。通过使用^运算符,可以找到多个集合之间的对称差集。
示例3:在symmetric_difference()方法中将iterable作为参数
#iterable is given as list
A={1,2,3}
B=[1]
print (A.symmetric_difference(B))#Output:{2,3}
#iterable is given as tuple
A={1,2,3}
B=(1,)
print (A.symmetric_difference(B))#Output:{2,3}
#iterable is given as range object
A={1,2,3}
B=range(2)
print (A.symmetric_difference(B))#Output:{2,3}
示例4:为^运算符提供参数iterable:
A={1,2,3}
B=[1]
A^B
print (A) #Output: TypeError: unsupported operand type(s) for ^: 'set' and'list'
‘symmetric_difference_update()’
更新集合,保留在两个集合中均找到的元素并去除两个集合中的公共元素。可以通过使用symmetric_difference_update()或使用^=运算符来实现,返回类型为None,将修改原始集本身。
语法:
symmetric_difference_update(other)
set ^= other
示例1:在A和B两组之间找到symmetric_difference_update()
将通过仅保留能在任一集合中找到,但不在两个集合中同时出现的元素来更新集合A。
#symmetric_difference_update()
A={1,2,3,4,5}
B={4,5,6,7,8}
print (A.symmetric_difference_update(B)) #Output: None
print (A) #Output: {1, 2, 3, 6, 7, 8}
A={1,2,3,4,5}
B={4,5,6,7,8}
A^=B
print (A) #Output: {1, 2, 3, 6, 7, 8}
‘isdisjoint()’
如果该集合没有共同元素,则返回True。当且仅当它们的交集为空集时,这时称集合之间无连接。
语法:
isdisjoint(other)
示例
#Set A and Set B containing common elements
A={1,2,3,4,5}
B={4,5,6,7,8}
print (A.isdisjoint(B))#Output:False
#Set A and Set B not containing common elements
A={1,2}
B={3,4}
print (A.isdisjoint(B))#Output:True
‘issubset()’
测试集合中的每个元素是否都在other元素中。
语法:
issubset(other)
set <= other
示例:检查集合A是否为集合B的子集
可以通过issubset()方法或使用≤运算符来完成。
A={1,2,3,4,5}
B={4,5,6,7,8}
print (A.issubset(B)) #Output: False
print (A<=B)#Output: False
A={1,2,3}
B={1,2,3,4,5}
print (A.issubset(B)) #Output: True
print (A<=B)#Output: False
Proper subset
测试集合是否为other的真子集,即set <= otherand set != other。
句法:
set < other
示例:检查集合A是否是B的真子集
如果两个集合相等,则意味着 A.issubsetset(B) 返回True,但是真子集A
A={1,2,3,4,5}
B={4,5,6,7,8}
print (A
A={1,2,3,4,5}
B={1,2,3,4,5}
print (A
A={1,2,3}
B={1,2,3,4,5}
print (A
‘issuperset()’
测试other中的每一个元素是否在集合中。
语法:
issuperset(other)
set >= other
示例:检查集合A是否为B的超集
可以通过issuperset()方法或使用≥运算符来实现:
A={1,2,3,4,5}
B={4,5,6,7,8}
print (A.issuperset(B)) #Output: False
print (A>=B)#Output:True
A={1,2,3,4,5}
B={1,2,3}
print (A.issuperset(B)) #Output: True
print (A>=B)#Output:True
Proper superset
测试集合是否是other集合的真超集,即,set >= otherand set != other。
语法:
set > other
示例:检查集合A是否为B的真超集。
如果两个集合相等,则意味着A.issuperset(B)返回True,但是真超集A> B将返回False。
A={1,2,3,4,5}
B={4,5}
print (A>B)#Output: True
A={1,2,3,4,5}
B={1,2,3,4,5}
print (A>B)#Output: False
A={1,2,3}
B={1,2,3,4,5}
print (A>B)#Output: True
总结
Frozenset不支持所有更新方法,frozenset类型不可变且不可哈希创建,一旦创建内容无法更改。由于所有更新方法都修改了原始集,所以frozenset不支持它。
我们可以通过两种方式执行数学集合设置操作:使用运算符或使用一种方法。其不同之处在于,如果使用方法,将接受iterable作为参数。但是对于运算符,仅应设置参数。如果不是,则会引发 TypeError。所有更新方法都会更新原始集,frozenset不支持该更新。除了更新方法外,所有其他方法都返回一个新集合。
留言点赞关注
我们一起分享AI学习与发展的干货
如转载,请后台留言,遵守转载规范
python集合的运算、不使用有的运算符_无序的集合:Python中的数学集合运算相关推荐
- 的python输入两个运算数及一个运算符_用Python解“两个数的简单计算器”题
7-12 两个数的简单计算器 本题要求编写一个简单计算器程序,可根据输入的运算符,对2个整数进行加.减.乘.除或求余运算.题目保证输入和输出均不超过整型范围. 输入格式: 输入在一行中依次输入操作数1 ...
- python对浮点类型的数据进行格式化_(自用)Python Log2 数据类型、字符编码、格式化...
数据类型 1.整数 十六进制可以使用0x+数字0-9(字母a-f). 2.浮点数 一般使用科学计数法,用e代替10,比如1.2e5,为1.2×10^5. 3.字符串 可以使用单引号' ',或者双引号& ...
- java中的 =运算符_(二十七)、java中的运算符
一.概述 java的运算符,分为四类: 算数运算符.关系运算符.逻辑运算符.位运算符 算术运算符(9):+ - * / % ++ -- 关系运算符(6):== != & ...
- python数据符号函数等一切皆对象_第一章:Python高级编程-Python一切皆对象
第一章:Python高级编程-Python一切皆对象 Python3高级核心技术97讲 笔记 1. Python一切皆对象 1.1 函数和类也是对象,属于Python的一等公民 "" ...
- python函数和模块有什么关键特性_零基础学python之函数与模块(附详细的代码和安装发布文件过程)...
代码重用--函数与模块 摘要:构建函数,创建模块,安装发布文件,安装pytest和PEP 8插件,确认PEP8兼容性以及纠错 重用代码是构建一个可维护系统的关键. 代码组是Python中对块的叫法. ...
- python编程快速上手-----让繁琐工作自动化_每周一书《Python编程快速上手 让繁琐工作自动化》分享!...
内容简介 如今,人们面临的大多数任务都可以通过编写计算机软件来完成.Python是一种解释型.面向对象.动态数据类型的高级程序设计语言.通过Python编程,我们能够解决现实生活中的很多任务. 本书是 ...
- 为什么python打开pygame秒关闭后在运行_当我运行Python程序时,pygame窗口打开片刻,然后退出 - python...
我是一个刚开始尝试通过在线课程使用python和pygame制作游戏的程序员.但是,当我运行以下代码时,pygame窗口将打开一秒钟,然后关闭. import pygame pygame.init() ...
- python json传参数可以传对象吗_廖雪峰的python系列教程(52)——IO编程之序列化...
序列化 在程序运行的过程中,所有的变量都是在内存中,比如,定义一个dict: d = dict(name='Bob', age=20, score=88) 可以随时修改变量,比如把name改成'Bil ...
- python是什么和c++是什么区别_编程c++和python的区别
展开全部 论坛 活动 招聘 专题 打开2113CSDN APP Copyright © 1999-2020, CSDN.NET, All Rights Reserved 登录 一颗日成 关注 浅谈52 ...
最新文章
- WindML相关知识和图形设备驱动程序开发(一)
- 【笨木头Lua专栏】基础补充08:协同程序之resume-yield间的数据返回
- 专访英特尔(中国)开源技术中心:HTML5要如何达到原生性能
- 详细介绍如何在win7下首次实现通过Git bash向Github提交项目
- sql 嵌套select与关联select
- VisualC++2010系列课程
- ionic 性能优化
- 不同业务场景下如何进行数据库水平切分?
- OPPO A37M刷机
- DPI-1047: Cannot locate a 64-bit Oracle Client library
- IDEA 社区版配置Tocat(超详细)
- SLAM学习 | 世界坐标系转经纬度误差分析
- java自动往数据库里插shuaku_x大x鸟的青鸟云课堂自动答题实现原理
- 一颗种子,一颗小树苗 在快速生长长大的过程中,遇到风雨在所难免
- JS 异步编程的解决方案,以及回调地狱的解决方案
- 通过源码理解 vue beforecreated 周期与 created 周期之间发生了什么
- 八、CSS3的美化背景与边框
- 宽带连接错误的处理办法651、691、623、678、645、720、721、718、734、769
- 95后程序员卧底诈骗群只为反诈
- Linux 大文件crc计算,Windows和Linux下使用MD5、SHA1、CRC32校验备份文件的完整性
热门文章
- 初学Linux应掌握的Shell命令
- 红帽linux iso镜像,红帽 Red Hat Linux相关产品iso镜像下载
- python返回列表中出现次数最多的数
- HTTP请求报文和响应报文中的实体数据
- Online DDL
- esp8266驱动oled屏幕_为什么“更好的OLED电视”在海信?
- Mybatis逆向生成报错:文档根元素 “project“ 必须匹配 DOCTYPE 根 “null“。
- 小学计算机考查方案,宋家塘街道中心学校2020年理化生实验操作和信息技术考试方案...
- 2d的公式_西师大版六年级数学上册全册必背公式+高清版电子课文,收藏预习
- python整商运算符_python中的运算符