python之集合set
1.测试
# python2和python3方法列表相同
ops23 = ['add', 'clear', 'copy', 'difference', 'difference_update', 'discard', 'intersection', 'intersection_update',
'isdisjoint', 'issubset', 'issuperset', 'pop', 'remove', 'symmetric_difference',
'symmetric_difference_update', 'union', 'update'] for op in ops23:
try:
print('#'*10)
S_abc = set('abc')
S_cde = set('cde')
print(op,eval('S_abc.{}(S_cde)'.format(op)))
print('S_abc:',S_abc)
print('S_cde:',S_cde)
except Exception as why:
print(op,why)
2. 总结
(1)元素操作
S.add 添加hashable type,注意set为unhashable type
S.pop随机删除并返回,remove删除指定元素可能报错,discard删除指定元素不会报错
S.clear 清空
S.copy 浅复制shallow copy
(2)判断关系
S.isdisjoint不相交 S.issubset子集 S.issuperset超集
(3)集合操作

| 命名方法 | 运算符语法 | 图例结果 | 对应update |
| S.difference(S1) | S-S1 | A | S -=S1 |
| S.intersection(S1) | S&S1 | B | S &=S1 |
| S.symmetric_difference(S1) | S^S1 | A+C | S ^=S1 |
| S.union(S1) | S|S1 | A+B+C | S |=S1 |
(a) S.xxx返回结果集合,S.xxx_update原地更新S,返回None,union对应方法为update而不是union_update
(b)命名方法相比运算符语法,S1可以是由可哈希的项目组成的任意迭代
>>> set('abc').union('cde')
set(['a', 'c', 'b', 'e', 'd'])
>>> set('abc').union(set('cde'))
set(['a', 'c', 'b', 'e', 'd'])
>>>
3.输出分析
##########
('add', TypeError("unhashable type: 'set'",))
##########
('clear', TypeError('clear() takes no arguments (1 given)',))
##########
('copy', TypeError('copy() takes no arguments (1 given)',))
Return a shallow copy of a set.
In [45]: S_abc.copy() == S_abc
Out[45]: True In [46]: S_abc.copy() is S_abc
Out[46]: False ##########
('difference', set(['a', 'b']))
('S_abc:', set(['a', 'c', 'b']))
('S_cde:', set(['c', 'e', 'd']))
##########
('difference_update', None)
('S_abc:', set(['a', 'b'])) #更新S1,留不同
('S_cde:', set(['c', 'e', 'd'])) ##########
('discard', None)
('S_abc:', set(['a', 'c', 'b']))
('S_cde:', set(['c', 'e', 'd'])) ##########
('intersection', set(['c']))
('S_abc:', set(['a', 'c', 'b']))
('S_cde:', set(['c', 'e', 'd']))
##########
('intersection_update', None)
('S_abc:', set(['c'])) #更新S1,留交集
('S_cde:', set(['c', 'e', 'd'])) ##########
('isdisjoint', False)
('S_abc:', set(['a', 'c', 'b']))
('S_cde:', set(['c', 'e', 'd']))
Return True if two sets have a null intersection.
##########
('issubset', False)
('S_abc:', set(['a', 'c', 'b']))
('S_cde:', set(['c', 'e', 'd']))
##########
('issuperset', False)
('S_abc:', set(['a', 'c', 'b']))
('S_cde:', set(['c', 'e', 'd'])) ##########
('pop', TypeError('pop() takes no arguments (1 given)',))
Remove and return an arbitrary set element.随机返回
##########
('remove', KeyError(set(['c', 'e', 'd']),))
########## ('symmetric_difference', set(['a', 'b', 'e', 'd']))
('S_abc:', set(['a', 'c', 'b']))
('S_cde:', set(['c', 'e', 'd']))
##########
('symmetric_difference_update', None)
('S_abc:', set(['a', 'b', 'e', 'd'])) #更新S1,所有异或 01 10
('S_cde:', set(['c', 'e', 'd'])) ##########
('union', set(['a', 'c', 'b', 'e', 'd'])) #并集
('S_abc:', set(['a', 'c', 'b']))
('S_cde:', set(['c', 'e', 'd']))
##########
('update', None) #更新S1为全集
('S_abc:', set(['a', 'c', 'b', 'e', 'd']))
('S_cde:', set(['c', 'e', 'd']))
python之集合set的更多相关文章
- Python 3 集合基础和概念!
Python 3 集合基础和概念! Python 3中,集合是无序的,所以不能进行切片和索引操作. 创建集合有两个方法:set()方法创建的集合是可变的,可被迭代的:frozenset()方法创建的集 ...
- Python的集合
1. Python的集合 1.1 集合的定义 在Python中, 集合set是基本数据类型的一种集合类型,它有可变集合(set())和不可变集合(frozenset)两种.Python中的集合set类 ...
- Python 操作集合
Python 操作集合 集合,set,主要用于数据的关系测试和去重处理,和列表类似,可以存储数据,列表中可以存储重复的数据,但是如果转化为集合之后,数据就会进行去重,然后保留唯一值:关系测试就是求多个 ...
- Python中集合set()的使用及处理
在Python中集合(set)与字典(dict)比较相似,都具有无序以及元素不能重复的特点 1.创建set 创建set需要一个list或者tuple或者dict作为输入集合 重复的元素在set中会被自 ...
- Python:集合操作总结
集合是一组无序排列的不重复元素集 [注]:集合的最大作用是对一个序列进行去重操作 一.集合的分类 在Python中集合分为两类,为可变集合(set)和不可变集合(frozenset).对于可变集合(s ...
- python 的集合 set()操作
Python 的集合 set(),是一个无序不重复元素集,可以用于关系测试和消除重复元素. 有以下运算: 1.创建一个set ()集合: 2.add:增加集合元素 3.clea ...
- python set集合(16)
在python变量中除了以前文章所提到的整形int / 浮点数float / 布尔值bool / 列表list / 字典dict 之外,还有一个类型我们还没有做详细介绍,这个变量类型就是集合set. ...
- python frozenset集合(17)
在前一篇文章中我们对 python set集合 做了详细的讲解,而本文讲解的 frozenset集合 其实和set集合类似!区别在于frozenset集合不能修改/添加/删除,其他功能和set集合一样 ...
- Python数据类型--集合(set)
Python的集合是无序.可迭代的容器对象,所有元素放在一对大括号中{},元素之间使用逗号隔开,同一集合内的元素具有唯一性,不允许重复. 集合中只能包含数字.字符串.元组等不可变类型的数据,不能包含列 ...
- [python]set集合学习
python的set和其他语言类似, 是一个无序不重复元素集, 基本功能包括关系测试和消除重复元素. 集合对象还支持union(联合), intersection(交), difference(差)和 ...
随机推荐
- Unity-Rigidbody碰撞穿透
首先,说说碰撞的条件:1.rigidbody(刚体),一般用在主动移动的物体上,比如角色.2.collider,碰撞器,一般用于受力物体上,比如障碍块. 发生概率即触发方式: 1.刚体速度足够快,被撞 ...
- LA 6893 矩阵HASH (模板)
#include<stdio.h> #include<string.h> typedef unsigned long long ULL; ; ; int test,n,m,x, ...
- 6)django-示例(fbv)
FBV(function base view),即一个url对应views.py一个函数 示例演示如下 1)FBV如何使用 2)渲染页面,并返回字典数据 3)字典数据页面如何访问 1)url.py f ...
- 饿了么vue-cli3.0+cube-ui笔记
1.目录结构 模板文件是public里的index.html,运行项目的时候,会引用src/main.js(入口文件) 详细文档在这里:https://cli.vuejs.org/zh/config/ ...
- Android组件化demo实现以及遇坑分享
首先贴出demo的github地址:GitHub - TenzLiu/TenzModuleDemo: android组件化demo 作者:TenzLiu原文链接:https://www.jianshu ...
- x学生管理系统(用中间件)-------基于FORM组件
目的:实现学生,老师,课程的增删改查 models.py from django.db import models # Create your models here. class UserInfo( ...
- tensorflow(3):神经网络优化(ema,regularization)
1.指数滑动平均 (ema) 描述滑动平均: with tf.control_dependencies([train_step,ema_op]) 将计算滑动平均与 训练过程绑在一起运行 train_o ...
- cut sticks
问题 : cut sticks 时间限制: 1 Sec 内存限制: 128 MB 题目描述 George took sticks of the same length and cut them ra ...
- Python属性(@property)
创建用于计算机的属性 在Python中,可以通过@property(装饰器)将一个方法转换为属性,从而实现用于计算的属性.将方法转换为属性后,可以直接通过方法名来访问方法,而不需要再添加一对小括号&q ...
- MySQL5.7.11版本,报错Cannot proceed because system tables used by Event Scheduler were found damaged at server start
解决思路: 1. 在MySQL安装目录下执行./mysql_upgrade -uroot -p,此处是为了更新MySQL的系统表,在5.6之前的版本上,更新系统表的命令是mysql_fix_privi ...