数据类型--集合

数据类型的种类

常用的:
1、数字
2、字符串
3、列表
4、元祖
5、集合

不常用的:
队列
有序字典
默认字典

集合的特点

1、无序
2、去重(重要)
3、可嵌套
4、关系测试(重要)

创建集合

(一)、回顾:创建列表的2中方法:

1、li = []  #直接创建
2、list() #可以将其他数据类型转换成列表(相当于在内存有创建一个)

注意:
     它会自动去执行list里面的构造方法__init__。
     list __init__,内部执行for循环(11,22,33,44)转换成[11,22,33,44]
     在定义  li=[]的时候,会内部调用list()这种方式。

一、创建集合的注意事项:

1、无序,不重复的序列

1 set = {11,22,11,22}
2 print(set)
3 
4 输出结果:
5 {11, 22}

2、用“{}”来代替,里面每个元素就是一个值,跟字典的区别就是无需key:values。
3、set的功能: set()  == list() 
4、将列表转换成集合的方法:

1 li = [11,22,11,22]
2 s1 = set(li)
3 print(s1,type(s1))
4 
5 显示结果:
6 {11, 22} <class 'set'>

二、创建集合的三种方式:

1、普通创建方法:

1 1、se = {11,22} 
2 例如:se = {"123","456"}

2、创建空集合:

1 s2 = set()             
2 这个是通过__init__进行转换的

3、转其他数据类型换成集合

1 s3 = set([11,22,33,4])

操作集合

一、添元素:

1 创建空集合:
2 s = set()
3 s.add(123)
4 s.add(123)
5 s.add(123)
6 print(s)
7 
8 显示结果:
9 {123}


注意:上面示例添加了3次123,但是只能显示一个,因为set集合有去重功能。
扩展:爬虫访问过的url放到一个set()中,可以通过去重功能,过滤掉访问过的url。

二、清空set里的所有内容:

1 s.clear()
2 print(s)
3 
4 显示结果:
5 set()

 

三、A中存在B中不存在的

注意:

A是指谁调用的表(s1的位置),
B就是被调用的表(s2的位置);
 1 #s1中存在s2中不存在的:
 2 s1 = {11,22,33}
 3 s2 = {22,33,44}
 4 s3 = s1.difference(s2)
 5 print(s3)
 6 
 7 
 8 #s2中存在s1中不存在的
 9 s3 = s2.difference(s1)
10 print(s3)
11 
12 显示结果:
13 {11}
14 {44}

四、取出A中存在,B中不存在的,然后将结果替换掉A中的元素:

1 s1.difference_update(s2)
2 print("s1:",s1)
3 
4 显示结果:
5 s1: {11}

注意:如果后续的代码不需要s1里面的元素时,可以使用带有Update方法来替换掉

五、对称差距
把A中存在的B中不存在的取出,
在把B中存在的A中不存在的取出;

 1 s1 = {11,22,33}
 2 s2 = {22,33,44}
 3 
 4 s3 = s1.symmetric_difference(s2)
 5 print(s3)
 6 print(s1)
 7 print(s2)
 8 
 9 显示结果:
10 {11, 44}
11 {33, 11, 22}
12 {33, 44, 22}

六、对称差距更新s1

取出A中存在B中不存在的元素,
取出B中存在A中不存在的元素,
将结果更新到A中

1 s1.symmetric_difference_update(s2)
2 print("更新s1",s1)
3 
4 显示结果:
5 更新s1 {11, 44}

注意:如果后续的代码不需要s1里面的元素时,可以使用带有Update方法来替换掉

七、移除元素:

#移除指定元素,不存在不报错:(以后推荐使用这个避免bug)
s1 = {11,22,33}
s1.discard(1111)
print("移除1111,数据不存在,不会报错:",s1)


#移除指定元素,如果不存在就直接报错。
s1 = {11,22,33}
s1.remove(22)
print("移除指定元素,有就不报错:",s1)

# s1.remove(111)
# print("移除指定元素,没有就报错:",s1)

#移除某个元素,并获取移除元素的值。(随机移除,不需要加参数,因为集合是无序的)
s1 = {11,22,33,44}
ret = s1.pop() #不用加参数
print("集合无序,随机移除,返回移除的数据",s1)
print("显示pop移除的元素:",ret)


显示结果:
移除1111,数据不存在,不会报错: {33, 11, 22}
移除指定元素,有就不报错: {33, 11}
集合无序,随机移除,返回移除的数据 {11, 44, 22}
显示pop移除的元素: 33

注意:使用pop的场景:以后再学到队列的时候会使用到,一般情况下移除一个元素,然后把移除的这个元素赋值到一个变量,然后在其他引用。

八、取出A和B之间的交集:

1 s1 = {11,22,33}
2 s2 = {22,33,44}
3 s3 = s1.intersection(s2)
4 print("取出s1和s2的交集:",s3)
5 
6 
7 显示结果:
8 取出s1和s2的交集: {33, 22}

九、取出A和B之间的交集,并更新到A里:

1 s1 = {11,22,33}
2 s2 = {22,33,44}
3 
4 s1.intersection_update(s2)
5 print("取出A和B之间的交集,并更新到A里:",s1)
6 
7 输出结果:
8 取出A和B之间的交集,并更新到A里: {33, 22}

十、判断是否为包含于被包含的关系:

 1 s1 = {11,22,33}
 2 s2 = {22,33}
 3 s3 = s1.issuperset(s2)
 4 print("s1是s2的父序列:",s3)
 5 
 6 s3 = s2.issubset(s1)
 7 print("s2是s1的子序列:",s3)
 8 
 9 
10 输出结果:
11 s1是s2的父序列: True
12 s2是s1的子序列: True

十一、取出A和B的并集:

1 s1 = {11,22,33}
2 s2 = {22,33,44}
3 s3 = s1.union(s2)
4 print("显示A和B的并集:",s3)
5 
6 显示结果:
7 显示A和B的并集: {33, 22, 11, 44}

十二、批量添加,批量更新(对可循环和可迭代的对象有效)

 1 #批量添加,批量更新(对可循环和可迭代的对象有效)
 2 s1 = {11,22,33}
 3 s1.add(44)
 4 s1.add(55)
 5 s1.add(66)
 6 print("往s1里添加记录",s1)
 7 
 8 #eg:列表
 9 li = [11,22,3,11,2]
10 s1.update(li)
11 print("将li列表中的元素加入到s1里:",s1)
12 
13 #eg:元祖
14 tuples = (66,77,8,10,2)
15 s1.update(tuples)
16 print("将元祖加入到s1里:",s1)
17 
18 #eg:字符串
19 st = "abiao"
20 s1.update(st)
21 print("将字符串加入到s1里:",s1)
22 
23 
24 显示结果:
25 往s1里添加记录 {33, 66, 11, 44, 22, 55}
26 将li列表中的元素加入到s1里: {33, 66, 3, 2, 11, 44, 22, 55}
27 将元祖加入到s1里: {33, 66, 3, 2, 8, 10, 11, 44, 77, 22, 55}
28 将字符串加入到s1里: {33, 66, 3, 2, 'o', 8, 'i', 10, 11, 44, 77, 'a', 'b', 22, 55}

集合的源代码:

  1 class set(object):
  2     """
  3     set() -> new empty set object
  4     set(iterable) -> new set object
  5      
  6     Build an unordered collection of unique elements.
  7     """
  8     def add(self, *args, **kwargs): # real signature unknown
  9         """
 10         Add an element to a set,添加元素
 11          
 12         This has no effect if the element is already present.
 13         """
 14         pass
 15  
 16     def clear(self, *args, **kwargs): # real signature unknown
 17         """ Remove all elements from this set. 清除内容"""
 18         pass
 19  
 20     def copy(self, *args, **kwargs): # real signature unknown
 21         """ Return a shallow copy of a set. 浅拷贝  """
 22         pass
 23  
 24     def difference(self, *args, **kwargs): # real signature unknown
 25         """
 26         Return the difference of two or more sets as a new set. A中存在,B中不存在
 27          
 28         (i.e. all elements that are in this set but not the others.)
 29         """
 30         pass
 31  
 32     def difference_update(self, *args, **kwargs): # real signature unknown
 33         """ Remove all elements of another set from this set.  从当前集合中删除和B中相同的元素"""
 34         pass
 35  
 36     def discard(self, *args, **kwargs): # real signature unknown
 37         """
 38         Remove an element from a set if it is a member.
 39          
 40         If the element is not a member, do nothing. 移除指定元素,不存在不保错
 41         """
 42         pass
 43  
 44     def intersection(self, *args, **kwargs): # real signature unknown
 45         """
 46         Return the intersection of two sets as a new set. 交集
 47          
 48         (i.e. all elements that are in both sets.)
 49         """
 50         pass
 51  
 52     def intersection_update(self, *args, **kwargs): # real signature unknown
 53         """ Update a set with the intersection of itself and another.  取交集并更更新到A中 """
 54         pass
 55  
 56     def isdisjoint(self, *args, **kwargs): # real signature unknown
 57         """ Return True if two sets have a null intersection.  如果没有交集,返回True,否则返回False"""
 58         pass
 59  
 60     def issubset(self, *args, **kwargs): # real signature unknown
 61         """ Report whether another set contains this set.  是否是子序列"""
 62         pass
 63  
 64     def issuperset(self, *args, **kwargs): # real signature unknown
 65         """ Report whether this set contains another set. 是否是父序列"""
 66         pass
 67  
 68     def pop(self, *args, **kwargs): # real signature unknown
 69         """
 70         Remove and return an arbitrary set element.
 71         Raises KeyError if the set is empty. 移除元素
 72         """
 73         pass
 74  
 75     def remove(self, *args, **kwargs): # real signature unknown
 76         """
 77         Remove an element from a set; it must be a member.
 78          
 79         If the element is not a member, raise a KeyError. 移除指定元素,不存在保错
 80         """
 81         pass
 82  
 83     def symmetric_difference(self, *args, **kwargs): # real signature unknown
 84         """
 85         Return the symmetric difference of two sets as a new set.  对称差集
 86          
 87         (i.e. all elements that are in exactly one of the sets.)
 88         """
 89         pass
 90  
 91     def symmetric_difference_update(self, *args, **kwargs): # real signature unknown
 92         """ Update a set with the symmetric difference of itself and another. 对称差集,并更新到a中 """
 93         pass
 94  
 95     def union(self, *args, **kwargs): # real signature unknown
 96         """
 97         Return the union of sets as a new set.  并集
 98          
 99         (i.e. all elements that are in either set.)
100         """
101         pass
102  
103     def update(self, *args, **kwargs): # real signature unknown
104         """ Update a set with the union of itself and others. 更新 """
105         pass
View Code
原文地址:https://www.cnblogs.com/abobo/p/8035235.html