简介

  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. [tensorflow源码分析] Conv2d卷积运算 (前向计算,反向梯度计算)

  2. redis持久化机制之AOF与RDB

    什么是redis Redis是一种面向“key-value”类型数据的分布式NoSQL数据库系统,具有高性能.持久存储.适应高并发应用场景等优势.它虽然起步较晚,但发展却十分迅速. redis为何需要 ...

  3. 版本管理工具git与svn简介

    版本管理工具 版本管理工具简介 常见版本管理工具 cvs(Concurrent Versions System) vss(Visual SourceSafe) svn 常用的版本管理工具 git 流行 ...

  4. Verilog 位拼接运算符的优先级

    最近研究FIFO的时候,在开源工程中看到这样一段代码 ; always @(posedge rd_clk) {'b0}}; else {'b0}}; else if(re) rp_bin <= ...

  5. FPGA中ROM与RAM相关知识总结(五)

    把看到的关于存储的一些东西整理一下,有些话来自于网友,所以还是那句话,看到的人要带着自己的思考去看,记住尽信书不如无书,fighting!!! 一.基本概念 最熟悉的两个词语应该是RAM与ROM,RA ...

  6. c++MFC工程修改在共享DLL中使用MFC为使用标准Windows库的解决办法

    由于创建MFC工程时,默认是在共享DLL中使用MFC,如果将此选项改成使用标准Windows库,会报如下错误 c:\program files\microsoft visual studio 9.0\ ...

  7. 控制 matplotlib 子图大小

    效果图: 代码: import numpy as np import matplotlib.pyplot as plt '''调整 matplotlib 子图的大小''' x1 = np.linspa ...

  8. 11- IO模型-未完成

    1.同步.异步.阻塞.非阻塞 同步(synchronous) IO和异步(asynchronous) IO,阻塞(blocking) IO和非阻塞(non-blocking)IO分别是什么,到底有什么 ...

  9. 5285: [Hnoi2018]寻宝游戏

    5285: [Hnoi2018]寻宝游戏 链接 分析: 从下面依次确定运算符号,然后在确定的过程中,需要确定的位数会逐渐减少.比如最后有一个1,如果在从下往上确定了一个or 1,那么再往前可以随便选了 ...

  10. Zabbix实战-简易教程--业务类

    一.需求 项目要求对线上服务器进行监控,包括服务器本身状态.进程相关数据.业务相关数据. 服务器本身状态可以通过基础模板即可获取数据(CPU.内存.网络.磁盘): 进程相关数据,前面也有相关文章专门监 ...