集合
python的set 和其它语言类似,是一个无序不重复元素集,基本功能包括关系测试和消除重复元素,集合对象还支持union(联合),intersection(交),difference(差)和sysmmetric difference(对称差集)等数学运算。
优点:
访问速度快
天生解决了重复问题
例子:
>>> s = set([1,2,3,4]) #创建一个数值集合 >>> sset([1, 2, 3, 4])>>> t = set("Hello") #创建一个唯一字符的集合与列表和元组不同,集合是无序的,也无法通过数字进行索引,而且集合里面的元素不能重复>>> tset(['H', 'e', 'l', 'o']) #发现只出现了一个l>>> a=t|s #t 和s 的并集>>> aset([1, 2, 3, 'e', 'H', 'l', 'o', 4])>>> b=t&s #t 和s 的交集>>> bset([])>>> c=t - s #求差集(在t,不在s)>>> cset(['H', 'e', 'l', 'o'])>>> d = t ^ s #对称差集(在t或s中,但不会同事出现在二者中)>>> dset([1, 2, 3, 4, 'H', 'l', 'o', 'e'])>>> tset(['H', 'e', 'l', 'o'])###基本操作>>> t.add('a') #添加一项>>> tset(['a', 'H', 'e', 'l', 'o']) >>> sset([1, 2, 3, 4])>>> s.update([90,34,53]) #在s中添加多项>>> sset([1, 34, 3, 4, 2, 53, 90])>>> tset(['a', 'H', 'e', 'l', 'o'])>>> t.remove('e') #删除>>> t set(['a', 'H', 'l', 'o'])>>> len(s) #set的长度7>>> len(t) #set 的长度4>>> 53 in s #测试x是否是s的成员True>>> 2 in s #测试x是否是s的成员True>>> 20 in s #测试x是否不是s的成员False>>> s.discard(90) #如果set中存在“90”元素,则删除>>> sset([1, 34, 3, 4, 2, 53])>>> s.pop() #删除并且返回集合中一个不确定的元素,如果集合空,则引发KeyError1>>> sset([34, 3, 4, 2, 53])>>> s.pop()34>>> sset([3, 4, 2, 53])>>> s.pop()3>>> sset([4, 2, 53])>>> s.pop()4>>> sset([2, 53])##为啥我测试每次都是删第一个元素的呢?>>> s.clear() #删除集合里的所有元素>>> sset([])>>> s=set([1, 34, 3, 4, 2, 53, 90])>>> sset([1, 34, 3, 4, 2, 53, 90])>>> tset(['a', 'H', 'l', 'o'])>>> s <= t #测试是否s中的每一个元素都在t中 (s.issubset(t))False>>> s >= t #测试是否t中的每一个元素都在s中 (s.issuperset(t))False>>> s.union(t) #返回一个新的set ,包含s和t中的每一个元素(s | t)set(['a', 1, 34, 3, 4, 'H', 2, 'o', 'l', 53, 90])>>> s.intersection(t) #返回一个新的set,包含s和t中的公共元素(s & t)set([])>>> s.difference(t) #返回一个新的set,包含s中有但是t中没有的元素(s - t)set([1, 34, 3, 4, 2, 53, 90])>>> s.symmetric_difference(t) #返回一个新的set,包含s和t中不重复的元素 (s ^ t)set(['a', 1, 34, 3, 4, 'H', 2, 'l', 'o', 53, 90])>>> new_set=s.copy #返回set的一个浅复制>>> new_set
稍稍记住:
s.union(t) = s | t : s和t的并集
s.intersection(t) = s & t : s和t的交集
s.difference(t) = s - t :差集(在s,不在t)
s.symmetric_difference(t) = s ^ t :对称差集(在t或s中,但不会同时出现在二者中,包含s和t中不重复的元素)
二.某些方法例子:
(1)difference、difference_update
def difference(self, *args, **kwargs): # real signature unknown """ 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
""" 删除当前set中的所有包含在 new set 里的元素 """ """ Remove all elements of another set from this set. """ pass
例子:
>>> s1set(['hy', 'alex', 'yaobin'])>>> s2set(['test2', 'hehe', 'hy', 'alex', 'yaobin'])>>> s4=s2.difference(s1)>>> s1set(['hy', 'alex', 'yaobin'])>>> s2set(['test2', 'hehe', 'hy', 'alex', 'yaobin'])>>> s4 #生成一个新的集合!set(['test2', 'hehe'])>>> >>> >>> s1set(['hy', 'alex', 'yaobin'])>>> s2set(['test2', 'hehe', 'hy', 'alex', 'yaobin'])>>> s5=s2.difference_update(s1)>>> s1set(['hy', 'alex', 'yaobin'])>>> s2 #把s1存在s2的元素清了!set(['test2', 'hehe'])>>> s5 #没有生成新的集合!>>> print s5None
(2)intersection、intersection_update
def intersection(self, *args, **kwargs): # real signature unknown """ 取交集,新创建一个set """ """ Return the intersection of two or more sets as a new set. (i.e. elements that are common to all of the sets.) """ pass def intersection_update(self, *args, **kwargs): # real signature unknown """ 取交集,修改原来set """ """ Update a set with the intersection of itself and another. """ pass
例子:
>>> s1set(['hy', 'alex', 'yaobin'])>>> s2set(['alex', 'hy', 'test2', 'hehe', 'yaobin'])>>> s3=s2.intersection(s1)>>> s3 #生成一个新的集合set(['hy', 'alex', 'yaobin'])>>> >>> >>> s1set(['hy', 'alex', 'yaobin'])>>> s2set(['alex', 'hy', 'test2', 'hehe', 'yaobin'])>>> s4=s2.intersection_update(s1)>>> s4 >>> print s4 #没有生成一个新的集合None>>> s1set(['hy', 'alex', 'yaobin'])>>> s2 #修改s2 去了!set(['hy', 'alex', 'yaobin'])
(3)isdisjoint
def isdisjoint(self, *args, **kwargs): # real signature unknown """ 如果没有交集,返回true """ """ Return True if two sets have a null intersection. """ pass 例子:
>>> s1set(['hy', 'alex', 'yaobin'])>>> s2set(['hy', 'alex', 'yaobin'])>>> s1.isdisjoint(s2)False>>> s2=set(['a','b','c'])>>> s1.isdisjoint(s2) True
(4)issubset、issuperset
def issubset(self, *args, **kwargs): # real signature unknown """ 是否是子集 """ """ Report whether another set contains this set. """ pass def issuperset(self, *args, **kwargs): # real signature unknown """ 是否是父集 """ """ Report whether this set contains another set. """ pass
例子:
>>> s1set(['hy', 'alex', 'yaobin'])>>> s2set(['haha', 'hy', 'alex', 'hehe', 'yaobin'])>>> s1.issubset(s2)True>>> >>> >>> s2.issuperset(s1)True
(5)pop、remove
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. """ pass 例子:
>>> s1set(['hy', 'alex', 'yaobin'])>>> s2set(['haha', 'hy', 'alex', 'hehe', 'yaobin'])>>> s2.pop() #pop可以拿一个元素,可以赋值给你定义的xxx'haha'>>> s2set(['hy', 'alex', 'hehe', 'yaobin'])>>> s2.remove()Traceback (most recent call last): File "", line 1, in TypeError: remove() takes exactly one argument (0 given)>>> s2.remove('hy') #要指定元素>>> s2set(['alex', 'hehe', 'yaobin'])
(6)symmetric_difference、symmetric_difference_update
def symmetric_difference(self, *args, **kwargs): # real signature unknown """ 对称差集,创建新对象""" """ 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. """ pass
例子:
>>> s1set(['hehe', 'hy', 'alex'])>>> s2set(['hehe', 'yaobin'])>>> s2.symmetric_difference(s1)set(['alex', 'hy', 'yaobin'])>>> s1set(['hehe', 'hy', 'alex'])>>> s2 #没变set(['hehe', 'yaobin'])>>> >>> >>> s2.symmetric_difference_update(s1)>>> s1set(['hehe', 'hy', 'alex'])>>> s2 #变了set(['hy', 'alex', 'yaobin'])>>>
(7) difference、symetric_difference
def difference(self, *args, **kwargs): # real signature unknown """ 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 symmetric_difference(self, *args, **kwargs): # real signature unknown """ 对称差集,创建新对象""" """ Return the symmetric difference of two sets as a new set. (i.e. all elements that are in exactly one of the sets.) """ pass
例子:
s1 = set([11,22,33])s2 = set([22,44])ret1 = s1.difference(s2)ret2 = s1.symmetric_difference(s2)print ret1print ret2结果:set([33, 11])set([33, 11, 44])解析: 可以理解为:ret1,先循环s1,判断不在s2的 ret2,先循环s1,判断不在s2的,后循环s2,判断不在s1的
end