1.集合的创建

集合是一个无序不重复元素的集。基本功能包括关系测试和消除重复元素。

创建集合:大括号或 set() 函数可以用来创建集合。注意:想要创建空集合,你必须使用 set() 而不是 {},后者用于创建空字典。大括号也不可以创建元素含有字典与列表的集合。

  1. a={'a','b','c','d'}
  2. b=set('abcdefabcd')
  3. c=set({'a':1,'b':2})
  4. d=set(['a','b','c','a'])
  5. print(a,type(a))
  6. print(b,type(b))
  7. print(c,type(c))
  8. print(d,type(d))
  9.  
  10. #运行结果
  11. {'c', 'd', 'b', 'a'} <class 'set'>
  12. {'f', 'e', 'b', 'c', 'd', 'a'} <class 'set'>
  13. {'b', 'a'} <class 'set'>
  14. {'c', 'b', 'a'} <class 'set'>

demo

2.集合的功能属性

  1. class set(object):
  2. """
  3. set() -> new empty set object
  4. set(iterable) -> new set object
  5.  
  6. Build an unordered collection of unique elements.
  7. """
  8. def add(self, *args, **kwargs): # real signature unknown
  9. """
  10. Add an element to a set.
  11.  
  12. This has no effect if the element is already present.
  13. """
  14. pass
  15.  
  16. def clear(self, *args, **kwargs): # real signature unknown
  17. """ Remove all elements from this set. """
  18. pass
  19.  
  20. def copy(self, *args, **kwargs): # real signature unknown
  21. """ Return a shallow copy of a set. """
  22. pass
  23.  
  24. def difference(self, *args, **kwargs): # real signature unknown
  25. """
  26. Return the difference of two or more sets as a new set.
  27.  
  28. (i.e. all elements that are in this set but not the others.)
  29. """
  30. pass
  31.  
  32. def difference_update(self, *args, **kwargs): # real signature unknown
  33. """ Remove all elements of another set from this set. """
  34. pass
  35.  
  36. def discard(self, *args, **kwargs): # real signature unknown
  37. """
  38. Remove an element from a set if it is a member.
  39.  
  40. If the element is not a member, do nothing.
  41. """
  42. pass
  43.  
  44. def intersection(self, *args, **kwargs): # real signature unknown
  45. """
  46. Return the intersection of two sets as a new set.
  47.  
  48. (i.e. all elements that are in both sets.)
  49. """
  50. pass
  51.  
  52. def intersection_update(self, *args, **kwargs): # real signature unknown
  53. """ Update a set with the intersection of itself and another. """
  54. pass
  55.  
  56. def isdisjoint(self, *args, **kwargs): # real signature unknown
  57. """ Return True if two sets have a null intersection. """
  58. pass
  59.  
  60. def issubset(self, *args, **kwargs): # real signature unknown
  61. """ Report whether another set contains this set. """
  62. pass
  63.  
  64. def issuperset(self, *args, **kwargs): # real signature unknown
  65. """ Report whether this set contains another set. """
  66. pass
  67.  
  68. def pop(self, *args, **kwargs): # real signature unknown
  69. """
  70. Remove and return an arbitrary set element.
  71. Raises KeyError if the set is empty.
  72. """
  73. pass
  74.  
  75. def remove(self, *args, **kwargs): # real signature unknown
  76. """
  77. Remove an element from a set; it must be a member.
  78.  
  79. If the element is not a member, raise a KeyError.
  80. """
  81. pass
  82.  
  83. def symmetric_difference(self, *args, **kwargs): # real signature unknown
  84. """
  85. Return the symmetric difference of two sets as a new set.
  86.  
  87. (i.e. all elements that are in exactly one of the sets.)
  88. """
  89. pass
  90.  
  91. def symmetric_difference_update(self, *args, **kwargs): # real signature unknown
  92. """ Update a set with the symmetric difference of itself and another. """
  93. pass
  94.  
  95. def union(self, *args, **kwargs): # real signature unknown
  96. """
  97. Return the union of sets as a new set.
  98.  
  99. (i.e. all elements that are in either set.)
  100. """
  101. pass
  102.  
  103. def update(self, *args, **kwargs): # real signature unknown
  104. """ Update a set with the union of itself and others. """
  105. pass
  106.  
  107. def __and__(self, *args, **kwargs): # real signature unknown
  108. """ Return self&value. """
  109. pass
  110.  
  111. def __contains__(self, y): # real signature unknown; restored from __doc__
  112. """ x.__contains__(y) <==> y in x. """
  113. pass
  114.  
  115. def __eq__(self, *args, **kwargs): # real signature unknown
  116. """ Return self==value. """
  117. pass
  118.  
  119. def __getattribute__(self, *args, **kwargs): # real signature unknown
  120. """ Return getattr(self, name). """
  121. pass
  122.  
  123. def __ge__(self, *args, **kwargs): # real signature unknown
  124. """ Return self>=value. """
  125. pass
  126.  
  127. def __gt__(self, *args, **kwargs): # real signature unknown
  128. """ Return self>value. """
  129. pass
  130.  
  131. def __iand__(self, *args, **kwargs): # real signature unknown
  132. """ Return self&=value. """
  133. pass
  134.  
  135. def __init__(self, seq=()): # known special case of set.__init__
  136. """
  137. set() -> new empty set object
  138. set(iterable) -> new set object
  139.  
  140. Build an unordered collection of unique elements.
  141. # (copied from class doc)
  142. """
  143. pass
  144.  
  145. def __ior__(self, *args, **kwargs): # real signature unknown
  146. """ Return self|=value. """
  147. pass
  148.  
  149. def __isub__(self, *args, **kwargs): # real signature unknown
  150. """ Return self-=value. """
  151. pass
  152.  
  153. def __iter__(self, *args, **kwargs): # real signature unknown
  154. """ Implement iter(self). """
  155. pass
  156.  
  157. def __ixor__(self, *args, **kwargs): # real signature unknown
  158. """ Return self^=value. """
  159. pass
  160.  
  161. def __len__(self, *args, **kwargs): # real signature unknown
  162. """ Return len(self). """
  163. pass
  164.  
  165. def __le__(self, *args, **kwargs): # real signature unknown
  166. """ Return self<=value. """
  167. pass
  168.  
  169. def __lt__(self, *args, **kwargs): # real signature unknown
  170. """ Return self<value. """
  171. pass
  172.  
  173. @staticmethod # known case of __new__
  174. def __new__(*args, **kwargs): # real signature unknown
  175. """ Create and return a new object. See help(type) for accurate signature. """
  176. pass
  177.  
  178. def __ne__(self, *args, **kwargs): # real signature unknown
  179. """ Return self!=value. """
  180. pass
  181.  
  182. def __or__(self, *args, **kwargs): # real signature unknown
  183. """ Return self|value. """
  184. pass
  185.  
  186. def __rand__(self, *args, **kwargs): # real signature unknown
  187. """ Return value&self. """
  188. pass
  189.  
  190. def __reduce__(self, *args, **kwargs): # real signature unknown
  191. """ Return state information for pickling. """
  192. pass
  193.  
  194. def __repr__(self, *args, **kwargs): # real signature unknown
  195. """ Return repr(self). """
  196. pass
  197.  
  198. def __ror__(self, *args, **kwargs): # real signature unknown
  199. """ Return value|self. """
  200. pass
  201.  
  202. def __rsub__(self, *args, **kwargs): # real signature unknown
  203. """ Return value-self. """
  204. pass
  205.  
  206. def __rxor__(self, *args, **kwargs): # real signature unknown
  207. """ Return value^self. """
  208. pass
  209.  
  210. def __sizeof__(self): # real signature unknown; restored from __doc__
  211. """ S.__sizeof__() -> size of S in memory, in bytes """
  212. pass
  213.  
  214. def __sub__(self, *args, **kwargs): # real signature unknown
  215. """ Return self-value. """
  216. pass
  217.  
  218. def __xor__(self, *args, **kwargs): # real signature unknown
  219. """ Return self^value. """
  220. pass
  221.  
  222. __hash__ = None

set

3.集合的部分功能属性介绍

1)add(self, *args, **kwargs):

在集合里添加一个元素,不生成新的集合。

  1. a={'a','b','c','d'}
  2. b=a.add('e')
  3. c=a.add('a')
  4. print(a,type(a))
  5. print(b,type(b))
  6. print(c,type(c))
  7.  
  8. #运行结果
  9. {'d', 'c', 'e', 'b', 'a'} <class 'set'>
  10. None <class 'NoneType'>
  11. None <class 'NoneType'>

demo

2)clear(self, *args, **kwargs):

清空集合里面的元素,不生成新的集合。

  1. a={'a','b','c','d'}
  2. b=a.clear()
  3. print(a,type(a))
  4. print(b,type(b))
  5.  
  6. #运行结果
  7. set() <class 'set'>
  8. None <class 'NoneType'>

demo

3)copy(self, *args, **kwargs):

浅拷贝集合,返回一个新集合。

  1. a={1,(9,2),3}
  2. b=a.copy()
  3. print(a,id(a))
  4. print(b,id(b))
  5.  
  6. #赋值
  7. c={1,2,3,4}
  8. d=c
  9. print(c,id(c))
  10. print(d,id(d))
  11.  
  12. #运行结果
  13. {(9, 2), 1, 3} 13058696
  14. {(9, 2), 1, 3} 13058576
  15. {1, 2, 3, 4} 13059296
  16. {1, 2, 3, 4} 13059296

demo

4)difference(self, *args, **kwargs):

与其他一个集合或多个集合对比,返回一个与其他集合不一样元素的集合。

  1. a={'a','b','c','d'}
  2. b=a.difference({'a',},{'b'})
  3. print(a)
  4. print(b,type(b))
  5.  
  6. #运行结果
  7. {'c', 'b', 'a', 'd'}
  8. {'c', 'd'} <class 'set'>

demo

5)difference_update(self, *args, **kwargs):

与其他一个集合或多个集合对比,删除集合与其他集合一样的元素,不返回新集合。

  1. a={'a','b','c','d'}
  2. b=a.difference_update({'a',},{'b'})
  3. print(a)
  4. print(b,type(b))
  5.  
  6. #运行结果
  7. {'c', 'd'}
  8. None <class 'NoneType'>

demo

6)discard(self, *args, **kwargs):

删除集合中的某个元素,如果这个元素没有在集合中,不做操作,不返回新集合。

  1. a={'a','b','c','d'}
  2. b=a.discard('a')
  3. print(a)
  4. print(b,type(b))
  5.  
  6. #运行结果
  7. {'d', 'c', 'b'}
  8. None <class 'NoneType'>

demo

7)intersection(self, *args, **kwargs):

返回一个和其他集合共同有的元素的集合。

  1. a={'a','b','c','d'}
  2. b=a.intersection({'a','e'},{'a','f'})
  3. print(a)
  4. print(b,type(b))
  5.  
  6. #运行结果
  7. {'d', 'b', 'c', 'a'}
  8. {'a'} <class 'set'>

demo

8)intersection_update(self, *args, **kwargs):

与其他一个集合或多个集合对比,删除集合中与其他集合不共有的元素,不返回新集合。

  1. a={'a','b','c','d'}
  2. b=a.intersection_update({'a','e'},{'a','f'})
  3. print(a)
  4. print(b,type(b))
  5.  
  6. #运行结果
  7. {'a'}
  8. None <class 'NoneType'>

demo

9)isdisjoint(self, *args, **kwargs):

对比两个集合,有空交集则返回True,没有则返回False。

  1. a={'a','b','c','d',' '} #空元素用tab键输入
  2. b=a.isdisjoint({'a','e'})
  3. c=a.isdisjoint({' ','f'})
  4. print(a)
  5. print(b,type(b))
  6. print(c,type(c))
  7.  
  8. #运行结果
  9. {'a', 'b', ' ', 'c', 'd'}
  10. False <class 'bool'>
  11. True <class 'bool'>

demo

10)issubset(self, *args, **kwargs):

判断集合的包含关系,其他集合如果包含原集合则返回True,不包含则返回Fasle。

  1. a={'a','b','c','d',}
  2. b={'a','b','c','d','f'}
  3. c=a.issubset(b)
  4. d=a.issubset({'a'})
  5. print(a)
  6. print(b)
  7. print(c,type(c))
  8. print(d,type(d))
  9.  
  10. #运行结果
  11. {'d', 'a', 'b', 'c'}
  12. {'f', 'b', 'a', 'd', 'c'}
  13. True <class 'bool'>
  14. False <class 'bool'>

demo

11)issuperset(self, *args, **kwargs):

判断集合的包含关系,原集合如果包含其他集合则返回True,不包含则返回Fasle。

  1. a={'a','b','c','d',}
  2. b={'a','b','c','d','f'}
  3. c=a.issuperset(b)
  4. d=a.issuperset({'a'})
  5. print(a)
  6. print(b)
  7. print(c,type(c))
  8. print(d,type(d))
  9.  
  10. #运行结果
  11. {'a', 'b', 'c', 'd'}
  12. {'a', 'b', 'd', 'c', 'f'}
  13. False <class 'bool'>
  14. True <class 'bool'>

demo

12)pop(self, *args, **kwargs):

从集合中取出一个元素,如果集合为空,则报TypeError错误。

  1. a={'a','b','c','d',}
  2. b=a.pop()
  3. print(a)
  4. print(b,type(b))
  5.  
  6. #运行结果
  7. {'c', 'b', 'a'}
  8. d <class 'str'> #因为集合是无序的,所以取出的值是随机的

demo

13)remove(self, *args, **kwargs):

移除集合中的一个元素,这个元素必须在集合中,如果不在,则报TypeError错误。

  1. a={'a','b','c','d',}
  2. b=a.remove('b')
  3. print(a)
  4. print(b,type(b))
  5.  
  6. #运行结果
  7. {'c', 'a', 'd'}
  8. None <class 'NoneType'>

demo

14)union(self, *args, **kwargs):

两个集合拼接返回一个新集合。

  1. a={'a','b','c','d',}
  2. b=a.union('b')
  3. c=a.union({'e','f'})
  4. print(a)
  5. print(b,type(b))
  6. print(c,type(c))
  7.  
  8. #运行结果
  9. {'b', 'd', 'c', 'a'}
  10. {'b', 'd', 'c', 'a'} <class 'set'>
  11. {'d', 'c', 'e', 'b', 'f', 'a'} <class 'set'>

demo

15)update(self, *args, **kwargs):

更新集合,添加集合中没有的新元素,不返回新集合。

  1. a={'a','b','c','d',}
  2. b=a.update('b')
  3. c=a.update({'e','f'})
  4. print(a)
  5. print(b,type(b))
  6. print(c,type(c))
  7.  
  8. #运行结果
  9. {'a', 'c', 'b', 'e', 'd', 'f'}
  10. None <class 'NoneType'>
  11. None <class 'NoneType'>

demo

16)__and__(self, *args, **kwargs):

和intersection()一样,返回一个和其他集合共同有的元素的集合,但是前者可以和多个集合一起对比,后者只可以和一个集合对比。

  1. a={'a','b','c','d',}
  2. b=a.__and__('b')
  3. c=a.__and__({'a','f'})
  4. print(a)
  5. print(b,type(b))
  6. print(c,type(c))
  7.  
  8. #运行结果
  9. {'a', 'b', 'd', 'c'}
  10. NotImplemented <class 'NotImplementedType'>
  11. {'a'} <class 'set'>

demo

17)__contains__(self, y):

判断集合中有没有包含某个元素或集合,包含则返回True,不包含则返回Fasle。

  1. a={'a','b','c','d',}
  2. b=a.__contains__('b')
  3. c=a.__contains__({'a','f'})
  4. print(a)
  5. print(b,type(b))
  6. print(c,type(c))
  7.  
  8. #运行结果
  9. {'a', 'd', 'c', 'b'}
  10. True <class 'bool'>
  11. False <class 'bool'>

demo

python 集合(set)的更多相关文章

  1. Python 集合set添加删除、交集、并集、集合操作符号

    在Python中集合set是基本数据类型的一种,它有可变集合(set)和不可变集合(frozenset)两种.创建集合set.集合set添加.集合删除.交集.并集.差集的操作都是非常实用的方法. 1. ...

  2. [转]python集合set

    Python中集合set是基本数据类型的一种,它有可变集合(set)和不可变集合(frozenset)两种.创建集合set.集合set添加.集合删除.交集.并集.差集的操作都是非常实用的方法. 来源网 ...

  3. python集合使用范例的代码

    在代码过程中中,将代码过程中比较好的代码段珍藏起来,如下的代码是关于python集合使用范例的代码,希望能对大伙有用. # sets are unordered collections of uniq ...

  4. python集合与字典的用法

    python集合与字典的用法 集合: 1.增加  add 2.删除   •del 删除集合 •discard(常用)删除集合中的元素  #删除一个不存在的元素不会报错 •remove 删除一个不存在的 ...

  5. Python 集合内置函数大全(非常全!)

    Python集合内置函数操作大全 集合(s).方法名 等价符号 方法说明 s.issubset(t) s <= t 子集测试(允许不严格意义上的子集):s 中所有的元素都是 t 的成员   s ...

  6. Python 集合set()添加删除、交集、并集、集合操作详解

    集合:一个集合中,任何两个元素都认为是不相同的,即每个元素只能出现一次.每个元素的地位都是相同的,元素之间是无序的. 创建集合set python set类是在python的sets模块中,大家现在使 ...

  7. python集合可以进行相减

    python集合可以进行相减 student = {'tom','jim','mary','tom','jack','rose'} print(student) print('rose' in stu ...

  8. Python集合类型的操作与应用

    Python集合类型的操作与应用 一.Python集合类型 Python中的集合类型是一个包含0个或多个数据项的无序的.不重复的数据组合,其中,元素类型只能是固定数据类型,如整数.浮点数.字符串.元组 ...

  9. Python - 集合 - 第十一天

    Python 集合 集合(set)是一个无序的不重复元素序列. 可以使用大括号 { } 或者 set() 函数创建集合,注意:创建一个空集合必须用 set() 而不是 { },因为 { } 是用来创建 ...

  10. python集合set,交集,并集,差集,对称差集,子集和超集

    python集合set,交集,并集,差集,对称差集,子集和超集 x = {1, 2, 3, 4} y = {2, 4, 5, 6} # 交集(取x中与y中相同部分) print(x.intersect ...

随机推荐

  1. SQL Injection简介

  2. ES6形参默认值

    * 形参的默认值----当不传入参数的时候默认使用形参里的默认值 function Point(x = ,y = ) { this.x = x; this.y = y; } //定义一个点的坐标 fu ...

  3. Jvm之class文件的加载、初始化

    编写的java文件在要真正运行时,会首先被编译成 “.class"结尾的二进制文件,然后被虚拟机加载.那么在虚拟机中一个class文件要成为java实例,需要经历好几个步骤: 一.class ...

  4. bootstrap 基础模板相关信息

    <!DOCTYPE html> <html> <head> <title></title> <link rel="style ...

  5. Js数据类型和运算符

    1.数据类型      原始类型:数值 字符串 布尔值: 复杂类型:对象:             数值(number)                      特殊的数值:NaN,NaN不等于任何 ...

  6. Ruby 类和对象

    Ruby 类和对象 Ruby 是一种完美的面向对象编程语言.面向对象编程语言的特性包括: 数据封装 数据抽象 多态性 继承 这些特性将在 面向对象的 Ruby 中进行讨论. 一个面向对象的程序,涉及到 ...

  7. 2018年阿里云NoSQL数据库大事盘点

    NoSQL一词最早出现在1998年.2009年Last.fm的Johan Oskarsson发起了一次关于分布式开源数据库的讨论,来自Rackspace的Eric Evans再次提出了NoSQL概念, ...

  8. php上传(一)

    php上传基础知识总结 1.文件上传 1>文件上传的原理:将客户端上的文件通过浏览器上传到服务器上, 在将服务器上的文件移动到指定目录. 2>客户端的配置 表单页面,浏览框 method= ...

  9. TopCoder[SRM587 DIV 1]:ThreeColorability(900)

    Problem Statement      There is a H times W rectangle divided into unit cells. The rows of cells are ...

  10. NX二次开发-获取按钮的ID UF_MB_ask_button_id

    NX9+VS2012 1.打开D:\Program Files\Siemens\NX 9.0\UGII\menus\ug_main.men 找到装配和PMI,在中间加上一段 TOGGLE_BUTTON ...