集合详解
集合特性:无序、去重
1.add
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
翻译:在集合里面增加一个元素
如果元素存在没影响
1 #!/usr/bin/python 2 #集合 3 test1=[0,1,2,3,4,5,5,6,6,'a','b','c'] 4 test2=['a','b','c','d','e','e','f',1,2,3] 5 x1=set(test1) 6 x2=set(test2) 7 print(x1) 8 print(x2) 9 print(dir(x1)) 10 x1.add(1) 11 print(x1)
2.clear
def clear(self, *args, **kwargs): # real signature unknown
""" Remove all elements from this set. """
pass
翻译:删除这个列表里面的所有元素
1 #!/usr/bin/python 2 #集合 3 test1=[0,1,2,3,4,5,5,6,6,'a','b','c'] 4 test2=['a','b','c','d','e','e','f',1,2,3] 5 x1.clear() 6 print(x1)
3.copy
def copy(self, *args, **kwargs): # real signature unknown
""" Return a shallow copy of a set. """
pass
翻译:返回集合的浅拷贝
1 #!/usr/bin/python 2 #集合 3 test1=[0,1,2,3,4,5,5,6,6,'a','b','c'] 4 test2=['a','b','c','d','e','e','f',1,2,3] 5 x1=set(test1) 6 x2=set(test2) 7 x3=x1.copy() 8 print(x3) 9 print(x1)
4.union |
def union(self, *args, **kwargs): # real signature unknown
"""
Return the union of sets as a new set.
(i.e. all elements that are in either set.)
"""
pass
翻译:返回与集合合并在一起的新集合
所有元素都在集合里面
1 #!/usr/bin/python 2 #集合 3 test1=[0,1,2,3,4,5,5,6,6,'a','b','c'] 4 test2=['a','b','c','d','e','e','f',1,2,3] 5 x3=x1.union(x2) #并集| 6 x4=x1|x2 7 print(x3) 8 print(x4)
5.update
def update(self, *args, **kwargs): # real signature unknown
""" Update a set with the union of itself and others. """
pass
翻译:用集合和另外一个集合的并集更新集合
1 #!/usr/bin/python 2 #集合 3 test1=[8,0,1,2,3,4,5,5,6,6,'a','b','c'] 4 test2=['a','b','c','d','e','e','f',1,2,3] 5 x1=set(test1) 6 x2=set(test2) 7 x1.update(x2) 8 print(x1)
6.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
翻译:返回一个两个或多个集合不同作为新的集合
所有元素在这个列表里面但是不在其他列表里面
1 #!/usr/bin/python 2 #集合 3 test1=[8,0,1,2,3,4,5,5,6,6,'a','b','c'] 4 test2=['a','b','c','d','e','e','f',1,2,3] 5 x1=set(test1) 6 x2=set(test2) 7 x3=x1.difference(x2) #差集- 8 x4=x1-x2 9 print(x3) 10 print(x4)
7.difference_update
def difference_update(self, *args, **kwargs): # real signature unknown
""" Remove all elements of another set from this set. """
pass
翻译:从这个列表里面移除所有在另外一个列表存在的元素
1 #!/usr/bin/python 2 #集合 3 test1=[8,0,1,2,3,4,5,5,6,6,'a','b','c'] 4 test2=['a','b','c','d','e','e','f',1,2,3] 5 x1=set(test1) 6 x2=set(test2) 7 x1.difference_update(x2) 8 print(x1)
8.intersection &
def intersection(self, *args, **kwargs): # real signature unknown
"""
Return the intersection of two sets as a new set.
(i.e. all elements that are in both sets.)
"""
pass
翻译:返回一个在两个集合里面的共有元素作为新的集合
所有元素在两个集合里面都存在
1 #!/usr/bin/python 2 #集合 3 test1=[8,0,1,2,3,4,5,5,6,6,'a','b','c'] 4 test2=['a','b','c','d','e','e','f',1,2,3] 5 x1=set(test1) 6 x2=set(test2) 7 x3=x1.intersection(x2) #交集& 8 x4=x1&x2 9 print(x3) 10 print(x4)
9.intersection_update
def intersection_update(self, *args, **kwargs): # real signature unknown
""" Update a set with the intersection of itself and another. """
pass
翻译:用这个集合和另外一个集合的元素更新这个集合
1 #!/usr/bin/python 2 #集合 3 test1=[0,1,2,3,4,5,5,6,6,'a','b','c'] 4 test2=['a','b','c','d','e','e','f',1,2,3] 5 x1.intersection_update(x2) 6 print(x1)
10.isdisjoint
def isdisjoint(self, *args, **kwargs): # real signature unknown
""" Return True if two sets have a null intersection. """
pass
翻译:如果两个字典没有交集,则返回True,否则返回False
1 #!/usr/bin/python 2 #集合 3 test1=[8,0,1,2,3,4,5,5,6,6,'a','b','c'] 4 test2=['a','b','c','d','e','e','f',1,2,3] 5 x1=set(test1) 6 x2=set(test2) 7 print(x1.isdisjoint(x2))#是否没有交集
11.issubset
def issubset(self, *args, **kwargs): # real signature unknown
""" Report whether another set contains this set. """
pass
翻译:是否另外一个集合包含这个集合
1 #!/usr/bin/python 2 #集合 3 test1=[0,1,2,3,4,5,5,6,6,'a','b','c'] 4 test2=['a','b','c'] 5 x1=set(test1) 6 x2=set(test2) 7 print(x1.issubset(x2))##x1是否是x2的子集 8 print(x2.issubset(x1))##x2是否是x1的子集
12.discard
def discard(self, *args, **kwargs): # real signature unknown
"""
Remove an element from a set if it is a member.
If the element is not a member, do nothing.
"""
pass
翻译:如果这个元素在集合里面就移除这个元素
如果这个元素不在里面,就不做任何操作
1 #!/usr/bin/python 2 #集合 3 test1=[0,1,2,3,4,5,5,6,6,'a','b','c'] 4 test2=['a','b','c','d','e','e','f',1,2,3] 5 x1=set(test1) 6 x2=set(test2) 7 x1.discard(1) 8 print(x1)
13.pop
def pop(self, *args, **kwargs): # real signature unknown
"""
Remove and return an arbitrary set element.
Raises KeyError if the set is empty.
"""
pass
翻译:移除和返回集合里面的任意元素
如果集合为空则抛出KeyError
1 #!/usr/bin/python 2 #集合 3 test1=[8,0,1,2,3,4,5,5,6,6,'a','b','c'] 4 test2=['a','b','c','d','e','e','f',1,2,3] 5 x1=set(test1) 6 x2=set(test2) 7 x1.pop() 8 print(x1)
14.remove
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
翻译:移除集合里面的元素,它必须是集合里面的,如果不在集合里面,则抛出异常KeyError
1 #!/usr/bin/python 2 #集合 3 test1=[8,0,1,2,3,4,5,5,6,6,'a','b','c'] 4 test2=['a','b','c','d','e','e','f',1,2,3] 5 x1=set(test1) 6 x2=set(test2) 7 x1.remove(1) 8 print(x1)
15.symmetric_difference^
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
翻译:返回2个集合对称差异作为一个新的集合
所有元素恰好在一个集合中,另外一个集合没有
1 #!/usr/bin/python 2 #集合 3 test1=[8,0,1,2,3,4,5,5,6,6,'a','b','c'] 4 test2=['a','b','c','d','e','e','f',1,2,3] 5 x1=set(test1) 6 x2=set(test2) 7 x3=x1.symmetric_difference(x2) 8 x4=x1^x2 9 print(x3) 10 print(x4)
16.symmetric_difference_update
def symmetric_difference_update(self, *args, **kwargs): # real signature unknown
""" Update a set with the symmetric difference of itself and another. """
pass
翻译:更新一个集合用它自己和另外一个集合的对称差异
1 #!/usr/bin/python 2 #集合 3 test1=[8,0,1,2,3,4,5,5,6,6,'a','b','c'] 4 test2=['a','b','c','d','e','e','f',1,2,3] 5 x1=set(test1) 6 x2=set(test2) 7 x1.symmetric_difference_update(x2) 8 print(x1)
人生没有白走的路,每一步都算数。
浙公网安备 33010602011771号