Python 集合(set)类型的操作——并交差
2016-02-15 13:46
661 查看
python的set是一个无序不重复元素集,基本功能包括关系测试和消除重复元素. 集合对象还支持并、交、差、对称差等。
sets 支持 x in set、 len(set)、和 for x in set。作为一个无序的集合,sets不记录元素位置或者插入点。因此,sets不支持 indexing, slicing, 或其它类序列(sequence-like)的操作。
回到顶部
基本操作
复制代码
>>> x = set("jihite")
>>> y = set(['d', 'i', 'm', 'i', 't', 'e'])
>>> x #把字符串转化为set,去重了
set(['i', 'h', 'j', 'e', 't'])
>>> y
set(['i', 'e', 'm', 'd', 't'])
>>> x & y #交
set(['i', 'e', 't'])
>>> x | y #并
set(['e', 'd', 'i', 'h', 'j', 'm', 't'])
>>> x - y #差
set(['h', 'j'])
>>> y - x
set(['m', 'd'])
>>> x ^ y #对称差:x和y的交集减去并集
set(['d', 'h', 'j', 'm'])
复制代码
回到顶部
函数操作
复制代码
>>> x
set(['i', 'h', 'j', 'e', 't'])
>>> s = set("hi")
>>> s
set(['i', 'h'])
>>> len(x) #长度
5
>>> 'i' in x
True
>>> s.issubset(x) #s是否为x的子集
True
>>> y
set(['i', 'e', 'm', 'd', 't'])
>>> x.union(y) #交
set(['e', 'd', 'i', 'h', 'j', 'm', 't'])
>>> x.intersection(y) #并
set(['i', 'e', 't'])
>>> x.difference(y) #差
set(['h', 'j'])
>>> x.symmetric_difference(y) #对称差
set(['d', 'h', 'j', 'm'])
>>> s.update(x) #更新s,加上x中的元素
>>> s
set(['e', 't', 'i', 'h', 'j'])
>>> s.add(1) #增加元素
>>> s
set([1, 'e', 't', 'i', 'h', 'j'])
>>> s.remove(1) #删除已有元素,如果没有会返回异常
>>> s
set(['e', 't', 'i', 'h', 'j'])
>>> s.remove(2)
Traceback (most recent call last):
File "<pyshell#29>", line 1, in <module>
s.remove(2)
KeyError: 2
>>> s.discard(2) #如果存在元素,就删除;没有不报异常
>>> s
set(['e', 't', 'i', 'h', 'j'])
>>> s.clear() #清除set
>>> s
set([])
>>> x
set(['i', 'h', 'j', 'e', 't'])
>>> x.pop() #随机删除一元素
'i'
>>> x
set(['h', 'j', 'e', 't'])
>>> x.pop()
'h'
sets 支持 x in set、 len(set)、和 for x in set。作为一个无序的集合,sets不记录元素位置或者插入点。因此,sets不支持 indexing, slicing, 或其它类序列(sequence-like)的操作。
回到顶部
基本操作
复制代码
>>> x = set("jihite")
>>> y = set(['d', 'i', 'm', 'i', 't', 'e'])
>>> x #把字符串转化为set,去重了
set(['i', 'h', 'j', 'e', 't'])
>>> y
set(['i', 'e', 'm', 'd', 't'])
>>> x & y #交
set(['i', 'e', 't'])
>>> x | y #并
set(['e', 'd', 'i', 'h', 'j', 'm', 't'])
>>> x - y #差
set(['h', 'j'])
>>> y - x
set(['m', 'd'])
>>> x ^ y #对称差:x和y的交集减去并集
set(['d', 'h', 'j', 'm'])
复制代码
回到顶部
函数操作
复制代码
>>> x
set(['i', 'h', 'j', 'e', 't'])
>>> s = set("hi")
>>> s
set(['i', 'h'])
>>> len(x) #长度
5
>>> 'i' in x
True
>>> s.issubset(x) #s是否为x的子集
True
>>> y
set(['i', 'e', 'm', 'd', 't'])
>>> x.union(y) #交
set(['e', 'd', 'i', 'h', 'j', 'm', 't'])
>>> x.intersection(y) #并
set(['i', 'e', 't'])
>>> x.difference(y) #差
set(['h', 'j'])
>>> x.symmetric_difference(y) #对称差
set(['d', 'h', 'j', 'm'])
>>> s.update(x) #更新s,加上x中的元素
>>> s
set(['e', 't', 'i', 'h', 'j'])
>>> s.add(1) #增加元素
>>> s
set([1, 'e', 't', 'i', 'h', 'j'])
>>> s.remove(1) #删除已有元素,如果没有会返回异常
>>> s
set(['e', 't', 'i', 'h', 'j'])
>>> s.remove(2)
Traceback (most recent call last):
File "<pyshell#29>", line 1, in <module>
s.remove(2)
KeyError: 2
>>> s.discard(2) #如果存在元素,就删除;没有不报异常
>>> s
set(['e', 't', 'i', 'h', 'j'])
>>> s.clear() #清除set
>>> s
set([])
>>> x
set(['i', 'h', 'j', 'e', 't'])
>>> x.pop() #随机删除一元素
'i'
>>> x
set(['h', 'j', 'e', 't'])
>>> x.pop()
'h'
相关文章推荐
- LIGO 的科学家们是如何使用 Python 语言的?
- Python 连接DBF数据库
- Python3 连接Access mdb数据库
- python笔记之字典的创建
- python编程常用模板总结
- python的编码
- python的编码
- Python装饰器
- 辨异 —— Python 的深拷贝与浅拷贝
- 转 python range 用法
- [Python]字典Dictionary、列表List、元组Tuple差异化理解
- 转在Python中实现PageFactory模式
- java调用python方法总结
- Python语言实践分析 -- With语句
- install opencv 3.0.0 with python 3.4 on Mac using Homebrew and Git
- java调用python方法总结
- 【python】编程语言入门经典100例--1
- python模块--random
- 基于python yield机制的异步操作同步化编程模型
- 利用python抓取京东的数据