1. 集合

  定义:集合是由不同元素组合而成,集合中是一组无序排列的可hash值,可做为字典的key。

  特征:集合的目的是将不同的值放在一起,不同的集合可以用来做关系运算。集合中的元素必须是不可变类型。可以帮助简单的去重。

1.1集合的创建

>>> {1,2,3,1}
{1, 2, 3}

或者

>>> set_test = set("hello")
>>> set_test
{'o', 'h', 'l', 'e'}

集合中的元素是不会重复的

frozenset()函数将集合改变为不可变集合

>>> f_set_test=frozenset(set_test)
>>> f_set_test
frozenset({'o', 'h', 'l', 'e'})

1.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         """
 11         Add an element to a set.
 12 
 13         This has no effect if the element is already present.
 14         """
 15         pass
 16 
 17     def clear(self, *args, **kwargs): # real signature unknown
 18         """清除集合内的所有元素"""
 19         """ Remove all elements from this set. """
 20         pass
 21 
 22     def copy(self, *args, **kwargs): # real signature unknown
 23         """浅拷贝集合内的元素"""
 24         """ Return a shallow copy of a set. """
 25         pass
 26 
 27     def difference(self, *args, **kwargs): # real signature unknown
 28         """求差集 相当于集合return a-b"""
 29         """
 30         Return the difference of two or more sets as a new set.
 31 
 32         (i.e. all elements that are in this set but not the others.)
 33         """
 34         pass
 35 
 36     def difference_update(self, *args, **kwargs): # real signature unknown
 37         """a.difference_update(b) 相当于 a = a - b"""
 38         """ Remove all elements of another set from this set. """
 39         pass
 40 
 41     def discard(self, *args, **kwargs): # real signature unknown
 42         """删除一个元素,元素不存在时不会报错"""
 43         """
 44         Remove an element from a set if it is a member.
 45 
 46         If the element is not a member, do nothing.
 47         """
 48         pass
 49 
 50     def intersection(self, *args, **kwargs): # real signature unknown
 51         """求两个集合的交集;相当于 return a & b"""
 52         """
 53         Return the intersection of two sets as a new set.
 54 
 55         (i.e. all elements that are in both sets.)
 56         """
 57         pass
 58 
 59     def intersection_update(self, *args, **kwargs): # real signature unknown
 60         """求两个集合的交集 更新;相当于 a = a & b"""
 61         """ Update a set with the intersection of itself and another. """
 62         pass
 63 
 64     def isdisjoint(self, *args, **kwargs): # real signature unknown
 65         """如果两个集合的交集为空,返回true"""
 66         """ Return True if two sets have a null intersection. """
 67         pass
 68 
 69     def issubset(self, *args, **kwargs): # real signature unknown
 70         """a.issubset(b) a是否是b的子集 相当于 a <= b"""
 71         """ Report whether another set contains this set. """
 72         pass
 73 
 74     def issuperset(self, *args, **kwargs): # real signature unknown
 75         """a.issubset(b) a是否是b的父集 相当于 a > b"""
 76         """ Report whether this set contains another set. """
 77         pass
 78 
 79     def pop(self, *args, **kwargs): # real signature unknown
 80         """随机删除一个元素"""
 81         """
 82         Remove and return an arbitrary set element.
 83         Raises KeyError if the set is empty.
 84         """
 85         pass
 86 
 87     def remove(self, *args, **kwargs): # real signature unknown
 88         """指定一个元素删除,只能有一个元素;删除元素不存在会报错"""
 89         """
 90         Remove an element from a set; it must be a member.
 91 
 92         If the element is not a member, raise a KeyError.
 93         """
 94         pass
 95 
 96     def symmetric_difference(self, *args, **kwargs): # real signature unknown
 97         """交叉差集; 两个集合;相当于先并集再去除他们相同的元素; a ^ b"""
 98         """
 99         Return the symmetric difference of two sets as a new set.
100 
101         (i.e. all elements that are in exactly one of the sets.)
102         """
103         pass
104 
105     def symmetric_difference_update(self, *args, **kwargs): # real signature unknown
106         """交叉差集更新; 两个集合;相当于先并集再去除他们相同的元素;a = a ^ b"""
107         """ Update a set with the symmetric difference of itself and another. """
108         pass
109 
110     def union(self, *args, **kwargs): # real signature unknown
111         """求并集 相当于 return a | b"""
112         """
113         Return the union of sets as a new set.
114 
115         (i.e. all elements that are in either set.)
116         """
117         pass
118 
119     def update(self, *args, **kwargs): # real signature unknown
120         """更新 元素,可以更新多个值,但是'int' object is not iterable;相当于 a = a | b"""
121         """ Update a set with the union of itself and others. """
122         pass
set

相关文章: