首页 > 代码库 > Python基本数据类型(三)
Python基本数据类型(三)
一、set的函数说明
集合(set)是一个无序不重复元素的序列,基本功能是进行成员关系测试和删除重复元素,可以使用大括号({})或者 set()函数创建集合;
注:创建一个空集合必须用set()而不是{ },因为{ }是用来创建一个空字典;
在python中set提供的函数如下:
class set(object): """ set() -> 空的新集合对象; set(iterable) -> 新的集合对象; Build an unordered collection of unique elements. """ def add(self, *args, **kwargs): # real signature unknown """ 在集合中增加元素,如果添加元素已存在于集合,则无效; 例如: >>> s1 = set() >>> s1.add(‘x‘) >>> s1 set([‘x‘]) """ pass def clear(self, *args, **kwargs): # real signature unknown """ 清空集合; 例如: >>> s1 = set([‘k1‘,‘k2‘]) >>> s1 set([‘k2‘, ‘k1‘]) >>> s1.clear() >>> s1 set([]) """ pass def copy(self, *args, **kwargs): # real signature unknown """ 集合的浅拷贝; 例如: >>> s1 = set([‘k1‘,‘k2‘]) >>> s2 = s1.copy() >>> s2 set([‘k2‘, ‘k1‘]) """ pass def difference(self, *args, **kwargs): # real signature unknown """ 获取两个集合的不同(差集),并生成一个新的集合; 即获取x.difference(y)的差集,相当于获取x多余y的集合值; 如果x包含于y,则获取空值; 例如: >>> s1 = set([‘a‘,‘b‘,‘c‘]) >>> s2 = set([‘a‘,‘b‘]) >>> s3 = s1.difference(s2) >>> s3 set([‘c‘]) >>> s4 = s2.difference(s1) >>> s4 set([]) """ pass def difference_update(self, *args, **kwargs): # real signature unknown """ 获取两个集合的不同(差集),改变原来的集合; 即获取x.difference_update(y)的差集,相当于获取x多余y的集合值,并重写进x; 如果x包含于y,则获取空值,并重写进x; 例如: >>> s1 = set([‘a‘,‘b‘,‘c‘]) >>> s2 = set([‘a‘,‘b‘]) >>> s1.difference_update(s2) >>> s1 set([‘c‘]) >>> s2 set([‘a‘, ‘b‘]) >>> s1 = set([‘a‘,‘b‘,‘c‘]) >>> s2 = set([‘a‘,‘b‘]) >>> s2.difference_update(s1) >>> s2 set([]) >>> s1 set([‘a‘, ‘c‘, ‘b‘]) """ pass def discard(self, *args, **kwargs): # real signature unknown """ 移除集合中的一个指定元素,如果这个元素不存在,则不变; 例如: >>> s1 = set([‘a‘,‘b‘,‘c‘]) >>> s1.discard(‘a‘) >>> s1 set([‘c‘, ‘b‘]) >>> s1.discard(‘d‘) >>> s1 set([‘c‘, ‘b‘]) """ pass def intersection(self, *args, **kwargs): # real signature unknown """ 获取两个集合的交集,生成一个新的集合; 即获取x.intersection(y)的交集,相当于获取x与y相等的那部分集合值; 例如: >>> s1 = set([‘a‘,‘b‘,‘c‘]) >>> s2 = set([‘a‘,‘b‘]) >>> s1.intersection(s2) set([‘a‘, ‘b‘]) >>> s1 set([‘a‘, ‘c‘, ‘b‘]) >>> s2 set([‘a‘, ‘b‘]) """ pass def intersection_update(self, *args, **kwargs): # real signature unknown """ 获取两个集合的交集,改变原来的集合; 即获取x.intersection_update(y)的交集,相当于获取x与y相等的那部分集合值,并重写进x; 如果x包含于y,则无变化; 例如: >>> s1 = set([‘a‘,‘b‘,‘c‘]) >>> s2 = set([‘a‘,‘b‘]) >>> s1.intersection_update(s2) >>> s1 set([‘a‘, ‘b‘]) >>> s1 = set([‘a‘,‘b‘,‘c‘]) >>> s2 = set([‘a‘,‘b‘]) >>> s2.intersection_update(s1) >>> s2 set([‘a‘, ‘b‘]) >>> s1 set([‘a‘, ‘c‘, ‘b‘]) """ pass def isdisjoint(self, *args, **kwargs): # real signature unknown """ 判断两个集合是否没有交集,如果是返回True,如果不是返回False; 例如: >>> s1 = set([‘a‘,‘b‘,‘c‘]) >>> s2 = set([‘a‘,‘b‘]) >>> s1.isdisjoint(s2) False >>> s2 = set([‘d‘]) >>> s1.isdisjoint(s2) True """ pass def issubset(self, *args, **kwargs): # real signature unknown """ 判断一个集合是否是另一个集合的子集; 即x.issubset(y),相当于判断x是否y的子集; 例如: >>> s1 = set([‘a‘,‘b‘,‘c‘]) >>> s2 = set([‘a‘,‘b‘]) >>> s1.issubset(s2) False >>> s2.issubset(s1) True """ pass def issuperset(self, *args, **kwargs): # real signature unknown """ 判断一个集合是否包含另一个集合; 即x.issuperset(y),相当于判断x是否包含y; 例如: >>> s1 = set([‘a‘,‘b‘,‘c‘]) >>> s2 = set([‘a‘,‘b‘]) >>> s1.issuperset(s2) True >>> s2.issuperset(s1) False """ pass def pop(self, *args, **kwargs): # real signature unknown """ 删除并返回任意设置的元素,如果集合为空,则引发KeyError; 例如: >>> s1 = set([‘a‘,‘b‘,‘c‘]) >>> s1.pop() ‘a‘ >>> s1.pop() ‘c‘ >>> s1.pop() ‘b‘ >>> s1.pop() Traceback (most recent call last): File "<stdin>", line 1, in <module> KeyError: ‘pop from an empty set‘ """ pass def remove(self, *args, **kwargs): # real signature unknown """ 移除集合中指定的元素,如果集合为空或指定的元素不存在,则引发KeyError; 例如: >>> s1 = set([‘a‘,‘b‘,‘c‘]) >>> s1.remove(‘b‘) >>> s1 set([‘a‘, ‘c‘]) >>> s1.remove(‘d‘) Traceback (most recent call last): File "<stdin>", line 1, in <module> KeyError: ‘d‘ """ pass def symmetric_difference(self, *args, **kwargs): # real signature unknown """ 把两个集合中的不同元素放到一个新的集合中; 例如: >>> s1 = set([‘a‘,‘b‘,‘c‘]) >>> s2 = set([‘c‘,‘d‘]) >>> s1.symmetric_difference(s2) set([‘a‘, ‘b‘, ‘d‘]) """ pass def symmetric_difference_update(self, *args, **kwargs): # real signature unknown """ 两个集合不相同的元素,并改变原集合; 例如: >>> s1 = set([‘a‘,‘b‘,‘c‘]) >>> s2 = set([‘c‘,‘d‘]) >>> s1.symmetric_difference_update(s2) >>> s1 set([‘a‘, ‘b‘, ‘d‘]) >>> s2 set([‘c‘, ‘d‘]) """ pass def union(self, *args, **kwargs): # real signature unknown """ 获取两个集合的并集,并生成一个新的集合; 例如: >>> s1 = set([‘a‘,‘b‘,‘c‘]) >>> s2 = set([‘c‘,‘d‘]) >>> s1.union(s2) set([‘a‘, ‘c‘, ‘b‘, ‘d‘]) >>> s1 set([‘a‘, ‘c‘, ‘b‘]) """ pass def update(self, *args, **kwargs): # real signature unknown """ 获取两个集合的并集,并生改变原集合; 例如: >>> s1 = set([‘a‘,‘b‘,‘c‘]) >>> s2 = set([‘c‘,‘d‘]) >>> s1.update(s2) >>> s1 set([‘a‘, ‘c‘, ‘b‘, ‘d‘]) >>> s2 set([‘c‘, ‘d‘]) """ pass def __and__(self, y): # real signature unknown; restored from __doc__ """ x.__and__(y) 等同于 x&y 集合与操作,相当于获取两个集合相同值,并进行返回; 例如: >>> s1 = set([‘a‘,‘b‘,‘c‘]) >>> s2 = set([‘c‘,‘d‘]) >>> s1.__and__(s2) set([‘c‘]) >>> s1 set([‘a‘, ‘c‘, ‘b‘]) >>> s2 set([‘c‘, ‘d‘]) >>> s1 = set([‘a‘,‘b‘,‘c‘]) >>> s2 = set([‘b‘,‘c‘]) >>> s1 & s2 set([‘c‘, ‘b‘]) """ pass def __cmp__(self, y): # real signature unknown; restored from __doc__ """ x.__cmp__(y) 等同于 cmp(x,y) 无意义 (Python2特有,Python3已删除) """ pass def __contains__(self, y): # real signature unknown; restored from __doc__ """ x.__contains__(y) 等同于 y in x. """ pass def __eq__(self, y): # real signature unknown; restored from __doc__ """ x.__eq__(y) 等同于 x==y """ pass def __getattribute__(self, name): # real signature unknown; restored from __doc__ """ x.__getattribute__(‘name‘) 等同于 x.name """ pass def __ge__(self, y): # real signature unknown; restored from __doc__ """ x.__ge__(y) 等同于 x>=y """ pass def __gt__(self, y): # real signature unknown; restored from __doc__ """ x.__gt__(y) 等同于 x>y """ pass def __iand__(self, y): # real signature unknown; restored from __doc__ """ x.__iand__(y) 等同于 x&=y """ 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, y): # real signature unknown; restored from __doc__ """ x.__ior__(y) 等同于 x|=y """ pass def __isub__(self, y): # real signature unknown; restored from __doc__ """ x.__isub__(y) 等同于 x-=y """ pass def __iter__(self): # real signature unknown; restored from __doc__ """ x.__iter__() 等同于 iter(x) """ pass def __ixor__(self, y): # real signature unknown; restored from __doc__ """ x.__ixor__(y) 等同于 x^=y """ pass def __len__(self): # real signature unknown; restored from __doc__ """ x.__len__() 等同于 len(x) """ pass def __le__(self, y): # real signature unknown; restored from __doc__ """ x.__le__(y) 等同于 x<=y """ pass def __lt__(self, y): # real signature unknown; restored from __doc__ """ x.__lt__(y) 等同于 x<y """ pass @staticmethod # known case of __new__ def __new__(S, *more): # real signature unknown; restored from __doc__ """ T.__new__(S, ...) -> a new object with type S, a subtype of T """ pass def __ne__(self, y): # real signature unknown; restored from __doc__ """ x.__ne__(y) 等同于 x!=y """ pass def __or__(self, y): # real signature unknown; restored from __doc__ """ x.__or__(y) 等同于 x|y """ pass def __rand__(self, y): # real signature unknown; restored from __doc__ """ x.__rand__(y) 等同于 y&x """ pass def __reduce__(self, *args, **kwargs): # real signature unknown """ Return state information for pickling. """ pass def __repr__(self): # real signature unknown; restored from __doc__ """ x.__repr__() 等同于 repr(x) """ pass def __ror__(self, y): # real signature unknown; restored from __doc__ """ x.__ror__(y) 等同于 y|x """ pass def __rsub__(self, y): # real signature unknown; restored from __doc__ """ x.__rsub__(y) 等同于 y-x """ pass def __rxor__(self, y): # real signature unknown; restored from __doc__ """ x.__rxor__(y) 等同于 y^x """ pass def __sizeof__(self): # real signature unknown; restored from __doc__ """ S.__sizeof__() -> size of S in memory, in bytes """ pass def __sub__(self, y): # real signature unknown; restored from __doc__ """ x.__sub__(y) 等同于 x-y """ pass def __xor__(self, y): # real signature unknown; restored from __doc__ """ x.__xor__(y) 等同于 x^y """ pass __hash__ = None
本文出自 “m199369309” 博客,请务必保留此出处http://434727.blog.51cto.com/424727/1894571
Python基本数据类型(三)
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。