首页 > 代码库 > 基本数据类型-集合(set)_上周内容回顾(列表_元组_集合)

基本数据类型-集合(set)_上周内容回顾(列表_元组_集合)

上周内容回顾

字符串
数字
列表
元组
字典

可变不可变:
1、可变:列表,字典
2、不可变: 字符串,数字,元组

访问顺序:
1、直接访问:数字
2、顺序访问:字符串,列表,元组
3、映射: 字典

存放元素个数:
容器类型:列表,元组,字典
原子:数字,字符串

 


set集合是一个无序而且不重复的集合,有些类似于数学中的集合,也可以求交集,求并集等

集合特性:
1、不同元素组成
2、无序
3、集合中元素必须是不可变类型

  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

 

set用法练习

 1 s1={1,2,3,1}                #定义一个set s1 如果s1={}为空则默认定义一个字典
 2 s2=set([2,5,6])             #定义一个set s2
 3 print(s1)                   #s1={1,2,3} 自动去除重复的元素
 4  
 5 s1.add(5)                   #s1={1,2,3,5} 添加一个元素
 6 print(s1)
 7  
 8 s3=s1.difference(s2)        #返回一个s1中存在而不存在于s2的字典s3,s3={1,3},而s1并没有改变
 9 print(s3)
10  
11 s1.difference_update(s2)    #s1跟新成上面的s3 s1={1,3}
12 s1.discard(1)               #删除元素1,不存在的话不报错  s1={3}
13 print(s1)
14 s1.remove(3)                #删除元素3,不存在的话报错    s1={}
15 print(s1)
16 s1.update([11,2,3])         #跟新s1中的元素,其实是添加   s1={11,2,3}
17 print(s1)
18 k=s1.pop()                  #删除一个元素,并将删除的元素返回给一个变量,无序的,所以并不知道删除谁
19  
20                   
21 s1={1,2,3,4}          #这里重新定义了集合s1,s2
22 s2={3,4,5,6}
23 r1=s1.intersection(s2)       #取交集,并将结果返回给一个新的集合 r1={3,4}
24 print(r1)
25 print(s1)
26 s1.intersection_update(s2)   #取交集,并将s1更新为取交集后的结果 s1={3,4}
27 print(s1)
28  
29 k1=s1.issubset(s2)           #s1是否是s2的的子序列是的话返回True,否则False 这里k1=true
30 print(k1)
31 k2=s1.issuperset(s2)         #s1是否是s2的父序列 k2=False
32  
33 k3=s2.isdisjoint(s1)         #s1,s2,是否有交集,有的话返回False,没有的话返回True
34 print(k3)
35 s1.update([1,2])             #s1={1,2,3,4}
36 r3=s1.union(s2)              #取并集将结果返回给r3 r3={1,2,3,4,5,6}
37 print(r3)
38 r2=s1.symmetric_difference(s2)          #r2=s1并s2-s1交s2  r2={1,2,5,6}
39 print(r2)
40 s1.symmetric_difference_update(s2)      #s1更新为 s1并s2 - s1交s2   s1={1,2,5,6}
41 print(s1)

 

基本数据类型-集合(set)_上周内容回顾(列表_元组_集合)