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

功能:

一、可以去掉列表,元组中的重复项

二、可以求交集,合集,差集等

def add(self, *args, **kwargs):

      """ 添加 """

s1 = (1,2,3,4,2,3,1)
s3 = set(s1)
s4 = s3.add(5)
print(s1)
print(s3)
print(s4)
(1, 2, 3, 4, 2, 3, 1)
{1, 2, 3, 4, 5}
None
def clear(self, *args, **kwargs):
        '''清除所有元素'''
s1 = (1,2,3,4,2,3,1)
s3 = set(s1)
s4 = s3.clear()
print(s1)
print(s3)
print(s4)
(1, 2, 3, 4, 2, 3, 1)
set()
Non
def copy(self, *args, **kwargs):
        '''复制一个set'''
s1 = (1,2,3,4,2,3,1)
s3 = set(s1)
s4 = s3.copy()
print(s1)
print(s3)
print(s4)
(1, 2, 3, 4, 2, 3, 1)
{1, 2, 3, 4}
{1, 2, 3, 4}
def difference(self, *args, **kwargs):
        '''a.difference(b) a和b求差集,显示a的结果'''
s1 = (1,2,3,4,2,3,1)
s2 = (3,4,5,7,1)
s5 = set(s2)
s3 = set(s1)
s4 = s3.difference(s2)
print(s3)
print(s5)
print(s4)
{1, 2, 3, 4}
{1, 3, 4, 5, 7}
{2}
def difference_update(self, *args, **kwargs):
      '''删除当前a中的所有包含在 b 里的元素,在原有集合中直接删除'''
s1 = (1,2,3,4,2,3,1)
s2 = (3,4,5,7,1)
s5 = set(s2)
s3 = set(s1)
print(s3)
s4 = s3.difference_update(s2)
print(s3)
print(s5)
print(s4)
{1, 2, 3, 4}
{2}
{1, 3, 4, 5, 7}
None
def discard(self, *args, **kwargs):
      '''删除集合中的元素,直接在原有集合中删除'''
s1 = (1,2,3,4,2,3,1)
s2 = (3,4,5,7,1)
s5 = set(s2)
s3 = set(s1)
print(s3)
s4 = s3.discard(2)
print(s3)
print(s5)
print(s4)
{1, 2, 3, 4}
{1, 3, 4}
{1, 3, 4, 5, 7}
None
def intersection(self, *args, **kwargs):
        """ 取交集,新创建一个set """
s1 = (1,2,3,4,2,3,1)
s2 = (3,4,5,7,1)
s5 = set(s2)
s3 = set(s1)
print(s3)
print(s5)
s4 = s3.intersection(s5)
print(s3)
print(s4)
{1, 2, 3, 4}
{1, 3, 4, 5, 7}
{1, 2, 3, 4}
{1, 3, 4}
def intersection_update(self, *args, **kwargs):
      '''取交集,修改原来set'''
s1 = (1,2,3,4,2,3,1)
s2 = (3,4,5,7,1)
s5 = set(s2)
s3 = set(s1)
print(s3)
print(s5)
s4 = s3.intersection_update(s5)
print(s3)
print(s4)
{1, 2, 3, 4}
{1, 3, 4, 5, 7}
{1, 3, 4}
None
def isdisjoint(self, *args, **kwargs):
        '''如果没有交集,返回true'''
s1 = (1,2,3,4,2,3,1)
s2 = (3,4,5,7,1)
s5 = set(s2)
s3 = set(s1)
print(s3)
print(s5)
s4 = s3.isdisjoint(s5)
print(s3)
print(s4)
{1, 2, 3, 4}
{1, 3, 4, 5, 7}
{1, 2, 3, 4}
False
def issubset(self, *args, **kwargs):
          '''是否是子集'''
def issuperset(self, *args, **kwargs):
           """ 是否是父集 """  

def pop(self, *args, **kwargs):
          '''移除'''
def remove(self, *args, **kwargs):            """ 移除 """   
def symmetric_difference(self, *args, **kwargs):
      """ 差集,创建新对象"""
def symmetric_difference_update(self, *args, **kwargs):
           """ 差集,改变原来 """   
def union(self, *args, **kwargs):
         """ 并集 """
def update(self, *args, **kwargs):
         """ 更新 """
 
 
      
  
  
 
  
    
      
      
      
上一篇:如何下载和安装CocoaPods


下一篇:如何用RadioButton做一个底部的切换栏