简介

  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 集合的更多相关文章

  1. Python 3 集合基础和概念!

    Python 3 集合基础和概念! Python 3中,集合是无序的,所以不能进行切片和索引操作. 创建集合有两个方法:set()方法创建的集合是可变的,可被迭代的:frozenset()方法创建的集 ...

  2. Python的集合

    1. Python的集合 1.1 集合的定义 在Python中, 集合set是基本数据类型的一种集合类型,它有可变集合(set())和不可变集合(frozenset)两种.Python中的集合set类 ...

  3. Python 操作集合

    Python 操作集合 集合,set,主要用于数据的关系测试和去重处理,和列表类似,可以存储数据,列表中可以存储重复的数据,但是如果转化为集合之后,数据就会进行去重,然后保留唯一值:关系测试就是求多个 ...

  4. Python中集合set()的使用及处理

    在Python中集合(set)与字典(dict)比较相似,都具有无序以及元素不能重复的特点 1.创建set 创建set需要一个list或者tuple或者dict作为输入集合 重复的元素在set中会被自 ...

  5. Python:集合操作总结

    集合是一组无序排列的不重复元素集 [注]:集合的最大作用是对一个序列进行去重操作 一.集合的分类 在Python中集合分为两类,为可变集合(set)和不可变集合(frozenset).对于可变集合(s ...

  6. python 的集合 set()操作

      Python 的集合 set(),是一个无序不重复元素集,可以用于关系测试和消除重复元素.     有以下运算:   1.创建一个set ()集合:   2.add:增加集合元素   3.clea ...

  7. python set集合(16)

    在python变量中除了以前文章所提到的整形int / 浮点数float / 布尔值bool / 列表list / 字典dict 之外,还有一个类型我们还没有做详细介绍,这个变量类型就是集合set. ...

  8. python frozenset集合(17)

    在前一篇文章中我们对 python set集合 做了详细的讲解,而本文讲解的 frozenset集合 其实和set集合类似!区别在于frozenset集合不能修改/添加/删除,其他功能和set集合一样 ...

  9. Python数据类型--集合(set)

    Python的集合是无序.可迭代的容器对象,所有元素放在一对大括号中{},元素之间使用逗号隔开,同一集合内的元素具有唯一性,不允许重复. 集合中只能包含数字.字符串.元组等不可变类型的数据,不能包含列 ...

  10. [python]set集合学习

    python的set和其他语言类似, 是一个无序不重复元素集, 基本功能包括关系测试和消除重复元素. 集合对象还支持union(联合), intersection(交), difference(差)和 ...

随机推荐

  1. http_load安装和使用

    一.安装 wget http://soft.vpser.net/test/http_load/http_load-12mar2006.tar.gz tar zxvf http_load-12mar20 ...

  2. js之checkbox判断常用示例

    checkbox常用示例可参考: 关于checkbox自动选中 checkbox选中并通过ajax传数组到后台接收 MP实战系列(十三)之批量修改操作(前后台异步交互) 本次说的是,还是关于智能门锁开 ...

  3. ZOJ 3203 Light Bulb (三分+计算几何)

    B - Light Bulb Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%lld & %llu Submit ...

  4. Angular动态表单生成(五)

    动态表单生成之布局 到上面的篇章为止,我们已经把表单比较完整的生成出来了,也实现了一些验证功能,可以说,我们截止这里,就已经可以满足我们的大部分表单生成需求了~ 但是: 目前来说,我们对于表单的布局只 ...

  5. C++反汇编代码分析–函数调用

    转载:http://shitouer.cn/2010/06/method-called/ 代码如下:#include “stdlib.h” int sum(int a,int b,int m,int ...

  6. 2017-2018-1 20155317 《信息安全系统设计基础》课堂实践——实现mypwd

    2017-2018-1 20155317 <信息安全系统设计基础>课堂实践——实现mypwd 1 . 学习使用pwd 很显然pwd命令的意思是打印出该文件当前的绝对路径 2 . 了解pwd ...

  7. 【转载】COM 组件设计与应用(十六)——连接点(vc.net)

    原文:http://vckbase.com/index.php/wv/1257.html 一.前言 上回书介绍了回调接口,在此基础上,我们理解连接点就容易多了. 二.原理 图一.连接点组件原理图.左侧 ...

  8. 12 stark组件之pop,按钮,url,页面

    1.Window open() 方法 http://www.runoob.com/jsref/met-win-open.html 效果图   2.admin的pop添加按钮 3.stark之pop功能 ...

  9. Java 原生日志 java.util.logging

    简介 Java 中的 Logging API 让 Java 应用可以记录不同级别的信息,它在debug过程中非常有用,如果系统因为各种各样的原因而崩溃,崩溃原因可以在日志中清晰地追溯,下面让我们来看看 ...

  10. linux安装anaconda3

    1,查看系统的版本  Uname –r 2,安装git 等依赖库 yum install git yum install zlib-devel bzip2-devel openssl-devel nc ...