python开发基础----数据类型(集合)
集合,为可变类型
定义: 由不同元素组成的集合,集合中是一组无序排列的hash值,可以作为字典的key
特性:
- 不同元素组成
- 无序
- 集合中元素必须是不可变类型
集合的目的是将不同的值存放到一起,不同的集合间用来做关系运算,无需纠结于集合中单个值
集合定义的方式
#集合定义的第一种方式
s = {1,2,3,4,2,3}
print(s) #输出结果是{1,2,3,4},相同的元素会自动剔除
#集合定义的第二种方式
s = set('hello')
print(s) #输出结果是{'o', 'l', 'h', 'e'}
s = set(['12',12])
改为不可变集合frozenset
f_set_test = frozenset('hello')
print(f_set_test)
# 结果为 frozenset({'o', 'e', 'l', 'h'})
集合常用操作:关系运算符
- in
- not in
- ==
- !=
- <,<=
- >,>=
- | :合集
- & :交集
- - :差集
- ^ :交叉补集
p_s = ['asd','qaz','lcq'] l_s = ['asd','qaz','swq'] p = set(p_s) l = set(l_s) #求交集,两个集合相同的元素 print(p & l) print(p.intersection(l)) #求并集,两个集合所有的元素,去除重复部分 print(p.union(l)) print(p | l) #差集 print(p - l) print(l - p) print(p.difference(l)) #交叉补集,用两个集合的并集减去交集(所有元素减去两个集合相同的元素) print(p ^ l) print(p.symmetric_difference(l))
集合的内置方法
class set(object): """ set() -> new empty set object set(iterable) -> new set object Build an unordered collection of unique elements. """ def add(self, *args, **kwargs): # real signature unknown """ Add an element to a set. This has no effect if the element is already present. 往集合中添加节点元素,如果元素已存在,不做任何操作 """ pass def clear(self, *args, **kwargs): # real signature unknown """ Remove all elements from this set. 清空集合 """ pass def copy(self, *args, **kwargs): # real signature unknown """ Return a shallow copy of a set. 复制 """ pass def difference(self, *args, **kwargs): # real signature unknown """ 相当于s1-s2,求差集 Return the difference of two or more sets as a new set. (i.e. all elements that are in this set but not the others.) """ pass def difference_update(self, *args, **kwargs): # real signature unknown """ Remove all elements of another set from this set. 相当于做完差集之后,把结果更新到集合,s1 = s1 - s2 """ pass def discard(self, *args, **kwargs): # real signature unknown """ 与remove功能相同,删除元素不存在时不会抛出异常 Remove an element from a set if it is a member. If the element is not a member, do nothing. """ pass def intersection(self, *args, **kwargs): # real signature unknown """ 相当于s1&s2 ,两个集合的交集(也就是相同的部分) Return the intersection of two sets as a new set. (i.e. all elements that are in both sets.) """ pass def intersection_update(self, *args, **kwargs): # real signature unknown """ Update a set with the intersection of itself and another. 两个集合做完交集后,把结果更新到集合, s1 = s1 & s2 """ pass def isdisjoint(self, *args, **kwargs): # real signature unknown """ Return True if two sets have a null intersection. 如果连个集合没有交集,就返回True """ pass def issubset(self, *args, **kwargs): # real signature unknown """ 相当于s1<=s2,相当于s1是s2的子集,就返回True Report whether another set contains this set. """ pass def issuperset(self, *args, **kwargs): # real signature unknown """ 相当于s1>=s2,相当于s1是s2的父集,就放回True Report whether this set contains another set. """ pass def pop(self, *args, **kwargs): # real signature unknown """ Remove and return an arbitrary set element. Raises KeyError if the set is empty. 随机删除集合中的一个节点元素 """ pass def remove(self, *args, **kwargs): # real signature unknown """ Remove an element from a set; it must be a member. If the element is not a member, raise a KeyError. 删除指定value的元素,如果集合中没有这个元素,会报KeyError的错误 """ pass def symmetric_difference(self, *args, **kwargs): # real signature unknown """ 相当于s1^s2 ,交叉补集,用两个集合的并集减去交集(所有元素减去两个集合相同的元素) Return the symmetric difference of two sets as a new set. (i.e. all elements that are in exactly one of the sets.) """ pass def symmetric_difference_update(self, *args, **kwargs): # real signature unknown """ Update a set with the symmetric difference of itself and another. 两个集合交叉补集的结果更新到集合,相当于 s1 = s1 ^ s2 """ pass def union(self, *args, **kwargs): # real signature unknown """ 相当于s1|s2,求并集,两个集合所有的元素,去除重复部分 Return the union of sets as a new set. (i.e. all elements that are in either set.) """ pass def update(self, *args, **kwargs): # real signature unknown """ Update a set with the union of itself and others. add只能添加一个值,update可更新多个值 """ pass def __and__(self, *args, **kwargs): # real signature unknown """ Return self&value. """ pass def __contains__(self, y): # real signature unknown; restored from __doc__ """ x.__contains__(y) <==> y in x. """ pass def __eq__(self, *args, **kwargs): # real signature unknown """ Return self==value. """ pass def __getattribute__(self, *args, **kwargs): # real signature unknown """ Return getattr(self, name). """ pass def __ge__(self, *args, **kwargs): # real signature unknown """ Return self>=value. """ pass def __gt__(self, *args, **kwargs): # real signature unknown """ Return self>value. """ pass def __iand__(self, *args, **kwargs): # real signature unknown """ Return self&=value. """ pass def __init__(self, seq=()): # known special case of set.__init__ """ set() -> new empty set object set(iterable) -> new set object Build an unordered collection of unique elements. # (copied from class doc) """ pass def __ior__(self, *args, **kwargs): # real signature unknown """ Return self|=value. """ pass def __isub__(self, *args, **kwargs): # real signature unknown """ Return self-=value. """ pass def __iter__(self, *args, **kwargs): # real signature unknown """ Implement iter(self). """ pass def __ixor__(self, *args, **kwargs): # real signature unknown """ Return self^=value. """ pass def __len__(self, *args, **kwargs): # real signature unknown """ Return len(self). """ pass def __le__(self, *args, **kwargs): # real signature unknown """ Return self<=value. """ pass def __lt__(self, *args, **kwargs): # real signature unknown """ Return self<value. """ pass @staticmethod # known case of __new__ def __new__(*args, **kwargs): # real signature unknown """ Create and return a new object. See help(type) for accurate signature. """ pass def __ne__(self, *args, **kwargs): # real signature unknown """ Return self!=value. """ pass def __or__(self, *args, **kwargs): # real signature unknown """ Return self|value. """ pass def __rand__(self, *args, **kwargs): # real signature unknown """ Return value&self. """ pass def __reduce__(self, *args, **kwargs): # real signature unknown """ Return state information for pickling. """ pass def __repr__(self, *args, **kwargs): # real signature unknown """ Return repr(self). """ pass def __ror__(self, *args, **kwargs): # real signature unknown """ Return value|self. """ pass def __rsub__(self, *args, **kwargs): # real signature unknown """ Return value-self. """ pass def __rxor__(self, *args, **kwargs): # real signature unknown """ Return value^self. """ pass def __sizeof__(self): # real signature unknown; restored from __doc__ """ S.__sizeof__() -> size of S in memory, in bytes """ pass def __sub__(self, *args, **kwargs): # real signature unknown """ Return self-value. """ pass def __xor__(self, *args, **kwargs): # real signature unknown """ Return self^value. """ pass __hash__ = None 查看 查看
浙公网安备 33010602011771号