Python set 集合
简介
python的set和其他语言类似, 是一个无序不重复元素集, 基本功能包括关系测试和消除重复元素. 集合对象还支持union(联 合), intersection(交), difference(差)和sysmmetric difference(对称差集)等数学运算.
创建集合
>>> S1 = set('spiritman')
>>> print S1
set(['a', 'i', 'm', 'n', 'p', 's', 'r', 't'])
集合常用操作及实例展示
可以使用dir(set)查看集合支持的操作方法
add
功能:增加一个元素到集合。当集合存在该元素时,该语句不生效
Add an element to a set.This has no effect if the element is already present.
语法:S.add(object)
实例展示:
>>> S1 = set('spirit')
>>> print S1
set(['i', 'p', 's', 'r', 't'])
>>> S1.add('a')
>>> print S1
set(['a', 'i', 'p', 's', 'r', 't'])
>>> S1.add('a')
>>> print S1
set(['a', 'i', 'p', 's', 'r', 't'])
clear
功能:清空集合
Remove all elements from this set.
语法:S.clear()
实例展示:
>>>S1 = set('spirit')
>>> print S1
set(['i', 'p', 's', 'r', 't'])
>>> S1.clear()
>>> print S1
set([])
copy
功能:浅复制集合,返回一个新的集合。
Return a shallow copy of a set.
语法:S.copy()
实例展示:
>>> S1 = set('spirit')
>>> print S1
set(['i', 'p', 's', 'r', 't'])
>>> S2 = S1.copy()
>>> print S2
set(['i', 'p', 's', 'r', 't'])
>>> id(S2)
140239642434120
>>> print S1
set(['i', 'p', 's', 'r', 't'])
>>> id(S1)
140239644667136
difference
功能:找出两个或多个集合中的不同元素,结果返回一个新的集合
Return the difference of two or more sets as a new set.(all elements that are in this set but not the others.)
语法:S.differencce(set1,set2.....)
实例展示:
>>> S1 = set('spirita')
>>> print S1
set(['a', 'i', 'p', 's', 'r', 't'])
>>> S2 = set('spiri')
>>> print S2
set(['i', 'p', 's', 'r'])
>>> S3 = set('liush')
>>> print S3
set(['i', 'h', 's', 'u', 'l'])
##################################################
#找出S1和S2中的不同元素,结果返回一个新的集合
>>> S1.difference(S2)
set(['a', 't'])
##################################################
#找出S1和S3中的不同元素,结果返回一个新的集合
>>> S1.difference(S3)
set(['a', 'p', 'r', 't'])
##################################################
#找出S1、S2和S3中的不同元素,结果返回一个新的集合
>>> S1.difference(S2,S3)
set(['a', 't'])
difference_update
功能:删除集合S中所有跟S1中相同的元素。无相同元素时,各个集合不会发生改变。
Remove all elements of another set from this set.
语法:S.difference_update(S1)
实例展示:
>>> S1 = set('spirit')
>>> print S1
set(['i', 'p', 's', 'r', 't'])
>>> S2 = set('abcde')
>>> print S2
set(['a', 'c', 'b', 'e', 'd'])
>>> S3 = set('spiritman')
>>> print S3
set(['a', 'i', 'm', 'n', 'p', 's', 'r', 't'])
>>> S1.difference_update(S2)
>>> print S1
set(['i', 'p', 's', 'r', 't'])
>>> print S2
set(['a', 'c', 'b', 'e', 'd'])
>>> S1.difference_update(S3)
>>> print S1
set([])
>>> print S3
set(['a', 'i', 'm', 'n', 'p', 's', 'r', 't'])
>>> S2.difference_update(S3)
>>> print S2
set(['c', 'b', 'e', 'd'])
>>> print S3
set(['a', 'i', 'm', 'n', 'p', 's', 'r', 't'])
discard
功能:从集合中删除一个元素。一次只能删除一个。
Remove an element from a set if it is a member.
语法:S.discard(object)
实例展示:
S3 = set('spiritman')
>>> print S3
set(['a', 'i', 'm', 'n', 'p', 's', 'r', 't'])
>>> S3.discard('a')
>>> print S3
set(['i', 'm', 'n', 'p', 's', 'r', 't'])
####################################################
#删除多个元素时报错
>>>S3.discard('i','m')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: discard() takes exactly one argument (2 given)
intersection
功能:求两个或多个集合的交集,交集返回一个新的集合。
Return the intersection of two or more sets as a new set.(elements that are common to all of the sets.)
语法:S.intersection(set1,set2...)
实例展示:
>>> S1 = set('spirit')
>>> print S1
set(['i', 'p', 's', 'r', 't'])
>>> S2 = set('spiman')
>>> print S2
set(['a', 'i', 'm', 'n', 'p', 's'])
>>> S3 = set('abcis')
>>> print S3
set(['a', 'i', 'c', 'b', 's'])
##################################################
#求S1和S2的交集
>>> S1.intersection(S2)
set(['i', 'p', 's'])
##################################################
#求S1和S3的交集
>>> S1.intersection(S3)
set(['i', 's'])
##################################################
#求S1、S2和S3的交集
>>> S1.intersection(S2,S3)
set(['i', 's'])
intersection_update
功能: 以一个集合和另一个集合的交集更新该集合。
Update a set with the intersection of itself and another.
语法:S.intersection_update(set1)
实例展示:
>>> S1 = set('spirit')
>>> print S1
set(['i', 'p', 's', 'r', 't'])
>>> S2 = set('spiman')
>>> print S2
set(['a', 'i', 'm', 'n', 'p', 's'])
>>> S3 = set('abcis')
>>> print S3
set(['a', 'i', 'c', 'b', 's'])
##################################################
#以S1和S3的交集更新S1
>>> S1.intersection_update(S3)
>>> print S1
set(['i', 's'])
##################################################
#以S2和S3的交集更新S2
>>> S2.intersection_update(S3)
>>> print S2
set(['a', 'i', 's'])
isdisjoint
功能:判断两个集合是否有集合。若没有则返回True;反之False。
Return True if two sets have a null intersection.
语法:S.isdisjoint(set1)
实例展示:
>>> S1 = set('spirit')
>>> print S1
set(['i', 'p', 's', 'r', 't'])
>>> S2 = set('spiman')
>>> print S2
set(['a', 'i', 'm', 'n', 'p', 's'])
>>> S3 = set('abcdef')
>>> print S3
set(['a', 'c', 'b', 'e', 'd', 'f'])
####################################################
>>> S1.isdisjoint(S2)
False
>>> S1.isdisjoint(S3)
True
>>> S2.isdisjoint(S3)
False
issubset
功能:判断集合是否是另一个集合的子集合。是则返回True;反之False.
Report whether another set contains this set.
语法:S.issubset(set1)
实例展示:
>>> S1 = set('spirit')
>>> print S1
set(['i', 'p', 's', 'r', 't'])
>>> S2 = set('spiritman')
>>> print S2
set(['a', 'i', 'm', 'n', 'p', 's', 'r', 't'])
>>> S3 = set('abcis')
>>> print S3
set(['a', 'i', 'c', 'b', 's'])
##################################################
>>> S1.issubset(S2)
True
>>> S3.issubset(S2)
False
>>> S1.issubset(S3)
False
issuperset
功能:判断集合是否包含另一个集合。
Report whether this set contains another set
语法:issuperset(set1)
实例展示:
>>> S1 = set('spiritman')
>>> print S1
set(['a', 'i', 'm', 'n', 'p', 's', 'r', 't'])
>>> S2 = set('spirit')
>>> print S2
set(['i', 'p', 's', 'r', 't'])
>>> S1.issuperset(S2)
True
pop
功能:删除并返回任意一个元素。
Remove and return an arbitrary set element.Raises KeyError if the set is empty.
语法:S.pop()
实例展示:
>>> S1 = set('spiritman')
>>> print S1
set(['a', 'i', 'm', 'n', 'p', 's', 'r', 't']) >>> S1.pop()
'a'
>>> S1.pop()
'i'
>>> S1.pop()
'm'
>>> print S1
set(['n', 'p', 's', 'r', 't'])
remove
功能:删除集合中一个指定的元素。
Remove an element from a set; it must be a member.If the element is not a member, raise a KeyError
语法:S.remove(object)
实例展示:
>>> S1 = set('spiritman')
>>> print S1
set(['a', 'i', 'm', 'n', 'p', 's', 'r', 't'])
>>> S1.remove('n')
>>> print S1
set(['a', 'i', 'm', 'p', 's', 'r', 't'])
>>> S1.remove('b')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
KeyError: 'b'
symmetric_difference
功能:取两个集合的差集,返回一个新集合。
Return the symmetric difference of two sets as a new set.(all elements that are in exactly one of the sets.)
语法:S.symmetric_difference(set1)
实例展示:
>>> S1 = set('spiritman')
>>> print S1
set(['a', 'i', 'm', 'n', 'p', 's', 'r', 't'])
>>> S2 = set('spirit')
>>> print S2
set(['i', 'p', 's', 'r', 't'])
>>> S3 = set('bcde')
>>> print S3
set(['c', 'b', 'e', 'd'])
##################################################
#返回S1和S2差集
>>> S1.symmetric_difference(S2)
set(['a', 'm', 'n'])
##################################################
#返回S1和S3差集
>>> S1.symmetric_difference(S3)
set(['a', 'c', 'b', 'e', 'd', 'i', 'm', 'n', 'p', 's', 'r', 't'])
##################################################
#返回S2和S3差集
>>> S2.symmetric_difference(S3)
set(['c', 'b', 'e', 'd', 'i', 'p', 's', 'r', 't'])
symmetric_difference_update
功能:取集合和另一个集合的差集,更新该集合。
Update a set with the symmetric difference of itself and another.
语法:symmetric_difference_update(set1)
实例展示:
>>> S1 = set('spiritman')
>>> print S1
set(['a', 'i', 'm', 'n', 'p', 's', 'r', 't'])
>>> S2 = set('spirit')
>>> print S2
set(['i', 'p', 's', 'r', 't'])
>>> S3 = set('bcde')
>>> print S3
set(['c', 'b', 'e', 'd'])
####################################################
>>> S1.symmetric_difference_update(S2)
>>> print S1
set(['a', 'm', 'n']) >>> S2.symmetric_difference_update(S3)
>>> print S2
set(['c', 'b', 'e', 'd', 'i', 'p', 's', 'r', 't'])
union
功能:取多个集合的并集,返回一个新的集合。
Return the union of sets as a new set.(all elements that are in either set.)
语法:S.union(set1,set2....)
实例展示:
>>> S1 = set('spiritman')
>>> print S1
set(['a', 'i', 'm', 'n', 'p', 's', 'r', 't'])
>>> S2 = set('spirit')
>>> print S2
set(['i', 'p', 's', 'r', 't'])
>>> S3 = set('bcde')
>>> print S3
set(['c', 'b', 'e', 'd'])
##################################################
#返回S1和S2的并集
>>> S1.union(S2)
set(['a', 'i', 'm', 'n', 'p', 's', 'r', 't'])
##################################################
#返回S1和S3的并集
>>> S1.union(S3)
set(['a', 'c', 'b', 'e', 'd', 'i', 'm', 'n', 'p', 's', 'r', 't'])
#################################################
#返回S1、S2和S3的并集
>>> S1.union(S2,S3)
set(['a', 'c', 'b', 'e', 'd', 'i', 'm', 'n', 'p', 's', 'r', 't'])
update
功能:取一个集合和另一个集合的并集,更新该集合。
Update a set with the union of itself and others.
语法:S.update(set1)
实例展示:
>>> S1 = set('spiritman')
>>> print S1
set(['a', 'i', 'm', 'n', 'p', 's', 'r', 't'])
>>> S3 = set('bcde')
>>> print S3
set(['c', 'b', 'e', 'd'])
>>> S1.update(S3)
>>> print S1
set(['a', 'c', 'b', 'e', 'd', 'i', 'm', 'n', 'p', 's', 'r', 't'])
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(差)和 ...
随机推荐
- 关于wordpress主题、插件上传和下载问题及其上传图片权限问题解决方案
主题官方下载地址:https://wordpress.org/themes/ 插件官方下载地址: https://wordpress.org/plugins/ 主题的上传下载,无疑是需要ftp服务器的 ...
- cmdb截图
- python 工具 eclipse pydev工具安装。
1.下载eclipse 2.下载java jre(这个会在运行eclipse的时候提示你下载,,根据系统型号下载就行) 3.下载完jre后,把目录下javaw.exe的路径添加到系统path环境变量中 ...
- centos安装telnet
安装环境:CentOS 6.4 上篇已经讲述了memcached的安装,现在要测试Memcached功能的时候,需要使用到telnet服务.于是就有了本篇. 一.安装telnet 1.检测t ...
- Linux 内存使用方法详细解析
我是一名程序员,那么我在这里以一个程序员的角度来讲解Linux内存的使用. 一提到内存管理,我们头脑中闪出的两个概念,就是虚拟内存,与物理内存.这两个概念主要来自于linux内核的支持. Linux在 ...
- Object-c SQLite 数据库内存溢出问题
最近正在开发一个应用,应用里面使用SQLite 数据库的地方比较多,一些下载的内容都进行了SQLite数据库缓存,应用开发完成之后发现一个严重的问题,程序莫名其妙的崩溃,使用XCode的内存分析工具分 ...
- #leetcode刷题之路34-在排序数组中查找元素的第一个和最后一个位置
给定一个按照升序排列的整数数组 nums,和一个目标值 target.找出给定目标值在数组中的开始位置和结束位置.你的算法时间复杂度必须是 O(log n) 级别.如果数组中不存在目标值,返回 [-1 ...
- 网格布局(GridLayout) 行数与列数
1.如果网格布局对象未指定具体的“行数”和“列数”,那么它将拥有1行和动态的列数. import java.awt.Button; import java.awt.Frame; import java ...
- Django:Admin后台网页标题和站点名称的修改
需要修改app的admin.py文件 #修改index的admin.py文件 from django.contrib import admin from index.models import * # ...
- rails框架配置
rails框架默认有三个模式development(开发),production(上线),test(测试) Development config.cache_classes = false 每次请求都 ...