Python的set集合浅析

set是一个无序且不重复的元素集合

set的优点:访问速度快;天生解决重复问题

部分源码分析如下:

1 def add(self, *args, **kwargs): # real signature unknown
2         """
3         Add an element to a set.
4         
5         This has no effect if the element is already present.
6         """
7         pass
 1 #练习1.添加
 2 s1 = set()
 3 s1.add("11,22,33")    #可以添加字符串和元组,不能添加列表,字典,每次只能添加一个
 4 s1.add("kkkkkkkk")
 5 print(s1)
 6 s1.add("11,22,33")      #添加重复字符串不生效
 7 s1.add(("k1","v1"))
 8 print(s1)
 9 
10 #执行结果:
11 {'11,22,33', 'kkkkkkkk'}
12 {'11,22,33', 'kkkkkkkk', ('k1', 'v1')}
注意:另外一种方法可以添加列表,字典
1 s2 = set([44,55,66,"aaa"])     #感觉像是赋值,因为后面被覆盖了
2 print(s2)
3 s2 = set({"kk":"vv","jj":"oo"})    #如果是字典取key
4 print(s2)
5 
6 执行结果:
7 {66, 'aaa', 44, 55}
8 {'kk', 'jj'}
1 def clear(self, *args, **kwargs): # real signature unknown
2         """ Remove all elements from this set. """
3         pass
 1 #练习2.清空set集合
 2 s1 = set()
 3 s1.add("11,22,33")    #可以添加字符串和元组,不能添加列表,字典,每次只能添加一个
 4 print(s1)
 5 s1.clear()     #清空set集合了
 6 print(s1)   
 7 
 8 #执行结果:
 9 {'11,22,33'}
10 set()
1 def difference(self, *args, **kwargs): # real signature unknown
2         """
3         Return the difference of two or more sets as a new set.
4         
5         (i.e. all elements that are in this set but not the others.)
6         """
7         pass
1 #练习3.比较不同的部分放到新集合
2 s2 = set(["aaa","bbb","ccc","ddd","aaa"])
3 print(s2)
4 ret = s2.difference(["aaa","bbb","kkk"])    #输出与指定集合不相同的部分放到新集合,无序
5 print(ret)
6 
7 执行结果:
8 {'aaa', 'ddd', 'bbb', 'ccc'}
9 {'ccc', 'ddd'}
def difference_update(self, *args, **kwargs): # real signature unknown
""" Remove all elements of another set from this set. """
pass
 1 #练习4.更新原有集合,移除相同部分
 2 s2 = set(["aaa","bbb","ccc","ddd","aaa"])
 3 print(s2)
 4 ret = s2.difference_update(["aaa","bbb"])    #更新原来的集合,移除相同部分,不生成集合
 5 print(s2)
 6 print(ret)      #不生成新集合,所以返回了None
 7 
 8 #执行结果:
 9 {'ddd', 'bbb', 'ccc', 'aaa'}
10 {'ccc', 'ddd'}
11 None
1 def discard(self, *args, **kwargs): # real signature unknown
2         """
3         Remove an element from a set if it is a member.
4         
5         If the element is not a member, do nothing.
6         """
7         pass
1 #练习5.移除集合里的单个元素
2 s2 = set(["aaa","bbb","ccc","ddd"])
3 print(s2)
4 s2.discard("aaa")     #移除一个元素,如果元素不存在,do nothing
5 print(s2)
6 
7 #执行结果:
8 {'aaa', 'ccc', 'bbb', 'ddd'}
9 {'ccc', 'bbb', 'ddd'}
 1 def intersection(self, *args, **kwargs): # real signature unknown
 2         """
 3         Return the intersection of two sets as a new set.
 4         
 5         (i.e. all elements that are in both sets.)
 6         """
 7         pass
 8 
 9     def intersection_update(self, *args, **kwargs): # real signature unknown
10         """ Update a set with the intersection of itself and another. """
11         pass
#练习6.取交集
s2 = set(["aaa","bbb","ccc","ddd"])
print(s2)
s3 = s2.intersection(["aaa"])     #原集合不变,取交集,并生成新集合
print(s3)
s2.intersection_update(["aaa"])    #更新原集合,移除原集合内容,放进交集
print(s2)

执行结果:
{'ddd', 'bbb', 'ccc', 'aaa'}
{'aaa'}
{'aaa'}
1 def isdisjoint(self, *args, **kwargs): # real signature unknown
2         """ Return True if two sets have a null intersection. """
3         pass
#练习6.判断是否有交集
s2 = set(["aaa","bbb","ccc","ddd"])
s3 = s2.isdisjoint(["aaa"])     #有交集则返回False
s4 = s2.isdisjoint(["kkk"])     #没交集则返回True
print(s3)
print(s4)

#执行结果:
False
True
1 def issubset(self, *args, **kwargs): # real signature unknown
2         """ Report whether another set contains this set. """
3         pass
4 
5     def issuperset(self, *args, **kwargs): # real signature unknown
6         """ Report whether this set contains another set. """
7         pass
 1 #练习7.判断父子集
 2 s2 = set(["aaa","bbb","ccc","ddd"])
 3 s3 = s2.issubset(["aaa"])     #判断这个集合是否包含原集合,即判断原集合是否子集
 4 s4 = s2.issuperset(["aaa"])     #判断原集合是否包含这个集合,即判断原集合是否父集
 5 print(s3)
 6 print(s4)
 7 
 8 #执行结果:
 9 False
10 True
 1 def pop(self, *args, **kwargs): # real signature unknown
 2         """
 3         Remove and return an arbitrary set element.
 4         Raises KeyError if the set is empty.
 5         """
 6         pass
 7 
 8     def remove(self, *args, **kwargs): # real signature unknown
 9         """
10         Remove an element from a set; it must be a member.
11         
12         If the element is not a member, raise a KeyError.
13         """
14         pass
 1 #练习8.随机删除和指定删除
 2 s2 = set(["aaa", "bbb", "ccc", "ddd"])
 3 s3 = s2.pop()  # 随机删除集合中的一个元素
 4 print(s2)
 5 s2 = set(["aaa", "bbb", "ccc", "ddd"])
 6 s4 = s2.remove("aaa")  # 指定删除集合中的一个元素
 7 print(s2)
 8 
 9 #执行结果:
10 {'ccc', 'ddd', 'aaa'}
11 {'bbb', 'ccc', 'ddd'}
 1     def symmetric_difference(self, *args, **kwargs): # real signature unknown
 2         """
 3         Return the symmetric difference of two sets as a new set.
 4         
 5         (i.e. all elements that are in exactly one of the sets.)
 6         """
 7         pass
 8 
 9     def symmetric_difference_update(self, *args, **kwargs): # real signature unknown
10         """ Update a set with the symmetric difference of itself and another. """
11         pass
 1 #练习9.差集
 2 s2 = set(["aaa", "bbb", "ccc", "ddd"])
 3 s3 = s2.symmetric_difference(["aaa","kkk"])  # 差集,并生成新集合,与difference区别是,循环多了一次,因此差集最好与交集混合用
 4 print(s3)
 5 s4 = s2.symmetric_difference_update(["bbb"])  # 差集,在原集合中改变
 6 print(s2)
 7 
 8 #执行结果:
 9 {'ccc', 'bbb', 'ddd','kkk'}
10 {'aaa', 'ccc', 'ddd'}
 1     def union(self, *args, **kwargs): # real signature unknown
 2         """
 3         Return the union of sets as a new set.
 4         
 5         (i.e. all elements that are in either set.)
 6         """
 7         pass
 8 
 9     def update(self, *args, **kwargs): # real signature unknown
10         """ Update a set with the union of itself and others. """
11         pass
 1 #练习10.并集和更新
 2 s2 = set(["aaa", "bbb", "ccc", "ddd"])
 3 s3 = s2.union(["aaa","kkk"])  # 并集,并生成新集合
 4 print(s3)
 5 s4 = s2.update(["bbb","jjj"])  # 更新,在原集合中改变
 6 print(s2)
 7 
 8 #执行结果:
 9 {'ccc', 'aaa', 'bbb', 'ddd', 'kkk'}
10 {'ccc', 'aaa', 'bbb', 'ddd', 'jjj'}
 
原文地址:https://www.cnblogs.com/repo/p/5420893.html