• 集合内置方法


    集合内置方法

    用途

    集合相当于数学中学的集合,将具有相性质的事务放在一起就是一个集合

    定义

    { }内用逗号分隔开多个元素,每个元素必须是不可变类型

    num_set1 = {1,2,3,5,8,4}
    num_set2 = set({1,2,5,79,34})
    alpha_set = set('abcbadea')
    
    print(num_set1)
    print(num_set2)
    print(alpha_set)
    
    {1, 2, 3, 4, 5, 8}
    {1, 2, 34, 5, 79}
    {'c', 'b', 'd', 'e', 'a'}
    

    集合的特点:

    • 无序:通过上面的num_set2,偶们是否可以观察到打印出来的集合和原来的集合顺序不一样了
    • 移除重复值:通过上面的alpha_set,可以看到打印的时候将原来集合里重复的值都移除了

    常用操作+内置方法

    优先掌握

    长度len

    语法:len(set())计算集合的长度,会自动移除重复值,然后再计算长度

    alpha_set = set('abcbadea')
    print(len(alpha_set))
    
    5
    

    成员运算in和not in

    alpha_set = {'c', 'b', 'd', 'e', 'a'}
    
    print('a' in alpha_set)
    print(5 in alpha_set)
    
    True
    False
    

    |并集、union

    语法:set1 | set2orset1.union(set2)返回所有集合的元素,重复的元素只会出现一次

    alpha_set = {'c', 'b'}
    num_set = {1,2,'b'}
    
    print(alpha_set | num_set)
    print(num_set.union(alpha_set))
    
    {1, 2, 'c', 'b'}
    {1, 2, 'c', 'b'}
    

    &交集、intersection

    语法:set1 & set2orset1.union(set2)返回两个或更多集合中都包含的元素,即交集

    alpha_set = {'c', 'b'}
    num_set = {1,2,'b'}
    
    print(alpha_set & num_set)
    print(num_set.intersection(alpha_set))
    
    {'b'}
    {'b'}
    

    -差集、difference

    语法:set1 - set2orset1.difference(set2)返回的集合元素包含在set1中,但不包含在set2中

    alpha_set = {'c', 'b'}
    num_set = {1,2,'b'}
    
    print(alpha_set - num_set)
    print(num_set.difference(alpha_set))
    
    {'c'}
    {1, 2}
    

    ^对称差集、symmetric_difference

    语法:set1 ^ set2orset1.symmetric_difference(set2)返回两个集合中不重复的元素集合

    alpha_set = {'c', 'b'}
    num_set = {1,2,'b'}
    
    print(alpha_set ^ num_set)
    print(num_set.symmetric_difference(alpha_set))
    
    {'c', 1, 2}
    {'c', 1, 2}
    

    ==

    语法:set1 == set2判断两个集合是否相同

    alpha_set = {'c', 'b'}
    num_set = {1,2,'b'}
    
    print(alpha_set == num_set)
    
    False
    

    父集:>、>= 、issuperset

    语法:set1 > set2orset1 >= set2or set1.issuperset(set2)判断set1是否包含set2

    alpha_set = {'c', 'b',1,2}
    num_set = {1,2,'b'}
    num_set1 = {1,'b',2}
    
    print(alpha_set > num_set)
    print(num_set.issuperset(num_set1))   # issuperset相当于 >=
    print(num_set > num_set1)
    
    True
    True
    False
    

    子集:<、<= 、issubset

    语法:set1 < set2orset1 <= set2or set1.issubset(set2)判断set2是否包含set1

    alpha_set = {'c', 'b',1,2}
    num_set = {1,2,'b'}
    num_set1 = {1,'b',2}
    
    print(num_set < alpha_set)
    print(num_set.issubset(num_set1))   # issubset相当于 <=
    print(num_set < num_set1)
    
    True
    True
    False
    

    需要掌握

    add

    语法:set.add()给集合添加元素,如果添加的元素在集合中已存在,则不执行任何操作

    num_set = {1,2,'b'}
    num_set.add('4')
    
    print(num_set)
    
    {1, 2, '4', 'b'}
    

    remove

    语法:set.remove()移除集合中的元素,如果集合中没有这个元素则会报错

    num_set = {1,2,'b','4'}
    num_set.remove('4')
    
    print(num_set)
    
    {1, 2, 'b'}
    
    num_set = {1,2,'b'}
    num_set.remove('4')   #  报错,集合中没有这个元素
    
    print(num_set)
    
    ---------------------------------------------------------------------------
    
    KeyError                                  Traceback (most recent call last)
    
    <ipython-input-45-c36b2e9b1ce9> in <module>
          1 num_set = {1,2,'b'}
    ----> 2 num_set.remove('4')   #  报错,集合中没有这个元素
          3 
          4 print(num_set)
    
    
    KeyError: '4'
    

    difference_update

    语法:set.difference_update(set1)移除两个集合中都存在的元素并将值返回给set,set1不会改变

    alpha_set = {'c', 'b',1,2}
    num_set = {1,2,'b'}
    alpha_set.difference_update(num_set)
    
    print(alpha_set)
    print(num_set)
    
    {'c'}
    {1, 2, 'b'}
    

    discard

    语法:set.dicard()和remove一样都是移除集合中的元素,但如果集合中没有这个元素则不会报错,remove会报错

    num_set = {1,2,'b','4'}
     
    num_set.discard('2')    # 不报错
    print(num_set)
    
    num_set.discard('4')
    print(num_set)
    
    {1, 2, '4', 'b'}
    {1, 2, 'b'}
    

    isdisjoint

    语法:set.isdisjoint()判断两个集合是否包含相同的元素,如果没有返回 True,否则返回 False

    alpha_set = {'c', 'b',1,2}
    num_set = {1,2,'b'}
    num_set1 = {'2'}
    print(num_set.isdisjoint(alpha_set))
    print(num_set1.isdisjoint(alpha_set))
    
    False
    True
    

    存一个值or多个值

    • 多个值

    有序or无序

    • 无序

    可变or不可变

    • 可变数据类型

    扩展

    拷贝

    相当于赋值操作,list2 = list1当一个list1的内容发生改变时,list2的值也会相应的发生改变

    list1 = ['a','b',[1,3,5]]
    list2 = list1
    print(list1,list2)
    list1.append('e')
    print(list1,list2)
    
    ['a', 'b', [1, 3, 5]] ['a', 'b', [1, 3, 5]]
    ['a', 'b', [1, 3, 5], 'e'] ['a', 'b', [1, 3, 5], 'e']
    

    浅拷贝

    浅拷贝是copy模块里的copy()函数,list2 = copy.copy(list1),当list1里的非子序列发生改变,list2不会发生改变,但是如果list1内的子序列发生变化,list2的对应子序列也会发生改变

    import copy
    
    list1 = ['a','b',[1,3,5]]
    list2 = copy.copy(list1)
    print(f'list1:{list1}
    list2:{list2}')
    
    list1.append('e')
    print(f'list1:{list1}
    list2:{list2}')     # list2没有发生变化
    
    list1[2].append(7)
    print(f'list1:{list1}
    list2:{list2}')     # list2的子序列发生变化
    
    list1:['a', 'b', [1, 3, 5]]
    list2:['a', 'b', [1, 3, 5]]
    list1:['a', 'b', [1, 3, 5], 'e']
    list2:['a', 'b', [1, 3, 5]]
    list1:['a', 'b', [1, 3, 5, 7], 'e']
    list2:['a', 'b', [1, 3, 5, 7]]
    

    深拷贝

    list2 = copy.deepcopy(list1),list1无论怎么改变,list2都不会发生变化

    import copy
    
    list1 = ['a','b',[1,3,5]]
    list2 = copy.deepcopy(list1)
    print(f'list1:{list1}
    list2:{list2}')
    
    list1.append('e')
    print(f'list1:{list1}
    list2:{list2}')     # list2没有发生变化
    
    list1[2].append(7)
    print(f'list1:{list1}
    list2:{list2}')     # list2的还是没有发生变化
    
    list1:['a', 'b', [1, 3, 5]]
    list2:['a', 'b', [1, 3, 5]]
    list1:['a', 'b', [1, 3, 5], 'e']
    list2:['a', 'b', [1, 3, 5]]
    list1:['a', 'b', [1, 3, 5, 7], 'e']
    list2:['a', 'b', [1, 3, 5]]
  • 相关阅读:
    C++结构体中sizeof
    sizeof()的用法
    XStream和Json
    省市联动
    ajax
    配置文件的读取
    JSP标签库
    字符串函数参数传入传出(去空格)
    字符串函数参数传入传出(字符串反转)
    opendir,readdir,closedir
  • 原文地址:https://www.cnblogs.com/Hades123/p/10840779.html
Copyright © 2020-2023  润新知