简介

  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'])

最新文章

  1. 2016 ACM赛后总结
  2. php部分---单文件上传的封装类
  3. Android系统目录介绍
  4. java动态代理浅析
  5. nyoj 4 ASCII码排序 java
  6. Java version 32转64位
  7. mysql apache php install
  8. jsp-------------之分页技术(一)
  9. Chrome中的消息循环
  10. php框架
  11. kibana-Request Timeout after 30000ms故障解决
  12. Centos 7 最小化kvm部署
  13. JSOUP 超时分析与处理
  14. DBUS 的学习 概念清晰
  15. Linux系统命令行中vim编辑器取消高亮显示
  16. Binary Tree Traversals(HDU1710)二叉树的简单应用
  17. C++ template —— 表达式模板(十)
  18. English trip -- FC(万词辩音王)
  19. JavaScript权威指南(第6版)(中文版)笔记
  20. SQL Server变量杂谈

热门文章

  1. JDBC通过配置文件(properites)读取数据库配置信息
  2. Angular动态表单生成(四)
  3. 你的安全设置不允许网站使用安装在你的计算机上的ActiveX控件
  4. 虹软人脸识别iOS SDK2.0
  5. 关于NSStringFromClass的一点见解
  6. iOS UITextField的代理&lt;UITextFieldDelegate&gt;的几点笔记
  7. canvas 绘制双线技巧
  8. laravel5.5源码笔记(八、Eloquent ORM)
  9. CQRS简单入门(Golang)
  10. set_new_handler