• 修炼Python基础篇-set学习


    Python的set和其他语言类似,是一个无序不重复元素集,基本功能包括关系测试和消除元素;


    1. add:添加一个元素到集合;

      1. a =set()
      2. a.add('aa')
      3. print(a)
      4. {'aa'}
    2. clear:清除集合里面所有的元素;

      1. a =set(['aa','bb',12,'aa'])
      2. print(a)
      3. {'aa',12,'bb'}
      4. a.clear()
      5. print(a)
      6. set()
    3. copy:浅复制集合生成一个新的集合;

      1. a =set(['aa','bb',12,'aa'])
      2. b = a.copy()
      3. print(b)
      4. {'aa',12,'bb'}
    4. difference:差集,返回由两个或多个集合中不同的元素组成一个新的集合;

      1. a = set(['aa','bb',11])
      2. b = set(['bb','cc',22])
      3. ret1 = a.difference(b) #a和b对比,返回a存在b不存在的元素;
      4. ret2 = b.difference(a) #b和a对比,返回b存在a不存在的元素;
      5. print(ret1)
      6. {11,'aa'}
      7. print(ret2)
      8. {22,'cc'}
    5. difference_update:差集,从当前集合中移除其它集合中所拥有的元素;

      1. a = set(['aa','bb',11])
      2. b = set(['bb','cc',22])
      3. ret1 = a.difference_update(b) #a和b对比,返回a中存在b不存在的元素,并更新自己的集合;
      4. print(a)
      5. {11,'aa'}
    6. discard:删除指定的元素,如果不存在不报错;

      1. a = set(['aa','bb',11])
      2. b = a.discard('aa')
      3. c = a.discard('dd')
      4. d = a.discard('')
      5. print(a)
      6. {11,'bb'}
      7. print(b)
      8. None
      9. print(c)
      10. None
      11. print(d)
      12. None
    7. intersection:交集,返回由两个或多个集合中相同的元素组成一个新的集合;

      1. a = set(['aa','bb',11])
      2. b = set(['bb','cc',22])
      3. ret1 = a.intersection(b)    #a和b相比,返回a存在b也存在的元素;
      4. ret2 = b.intersection(a) #b和a对比,返回b存在a也存在的元素;
      5. print(a)
      6. {11, 'aa', 'bb'}
      7. print(b)
      8. {'cc', 22, 'bb'}
      9. print(ret1)
      10. {'bb'}
      11. print(ret2)
      12. {'bb'}

       

    8. intersection_update:交集,从当前集合中移除其它集合中不拥有的元素;

      1. a = set(['aa','bb',11])
      2. b = set(['bb','cc',22])
      3. ret1 = a.intersection_update(b)
      4. print(a)
      5. {'bb'}
      6. print(ret1)
      7. None
    9. isdisjoint:如果两个集合没有交集返回True;
      1. a = set(['aa','bb',11])
      2. b = set(['aa','cc',22])
      3. x = set(['dd','cc',22])
      4. a.isdisjoint(b)    #a与b是否有交集,有返回False;
      5. False
      6. a.isdisjoint(x)    #a与x是否有交集,没有返回True;
      7. True
      8. b.isdisjoint(a)
      9. False
      10. x.isdisjoint(a)
      11. True
    10. issubset:判断a集合是否是b集合的子集;
      1. a = set(['aa','bb',11])
      2. b = set(['aa','bb',11,'dd'])
      3. c = set(['bb',22])
      4. a.issubset(b)    #a是否是b的子集,是则返回True;
      5. True
      6. c.issubset(b)    #c是否是b的子集,不是则返回False;
      7. False
    11. issuperset:判断a集合是否是b集合的父集;
      1. a = set(['aa','bb',11,'cc',22])
      2. b =set(['aa',11])
      3. c =set(['aa',11,'dd'])
      4. a.issuperset(b)    #a是否是b的父集,是则返回True;
      5. True
      6. a.issuperset(c)    #a是否是c的父集,不是则返回False;
      7. False
    12. pop:随机删除集合中的一个元素;
      1. a = set(['aa','bb',11,'cc',22])
      2. print(a)
      3. {'cc',11,'aa',22,'bb'}
      4. a.pop()
      5. 'cc'
      6. print(a)
      7. {11,'aa',22,'bb'}
    13. remove:删除集合中指定的一个元素,如果不存在不报错;
      1. a = set(['aa','bb',11,'cc',22])
      2. a.remove('aa')
      3. print(a)
      4. {'cc',11,22,'bb'}
    14. symmetric_difference:对称差,返回两个集合的差集,生成新的集合;
      1. a = set(['aa','bb',11])
      2. b = set(['aa,','cc',22])
      3. ret1 = a.symmetric_difference(b)
      4. ret2 = b.symmetric_difference(a)
      5. print(ret1)
      6. {'bb',11,'aa','aa,',22,'cc'}
      7. print(ret2)
      8. {'bb',11,'aa','aa,',22,'cc'}
      9. print(a)
      10. {11,'aa','bb'}
      11. print(b)
      12. {'aa,',22,'cc'}
    15. symmetric_difference_update:对称差,返回两个几个的差集,不生成新的集合;
      1. a = set(['aa','bb',11])
      2. b = set(['aa,','cc',22])
      3. ret1 = a.symmetric_difference_update(b)
      4. print(ret1)
      5. None
      6. print(a)
      7. {'bb',11,'aa','aa,',22,'cc'}
    16. union:返回a和b的并集;
      1. a = set(['aa','bb',11])
      2. b = set(['aa,','cc',22])
      3. c = a.union(b)
      4. print(c)
      5. {'aa,',22,'bb',11,'aa','cc'}
    17. update:更新自身集合;
      1. a = set(['aa','bb',11])
      2. b = set(['aa,','cc',22])
      3. b.update(a)
      4. print(b)
      5. {'aa,',22,'bb',11,'aa','cc'}
  • 相关阅读:
    ADO.NET连接SQLSERVER数据库 连接字符串
    HttpModule的作用
    MyBatis框架简介
    idea成功部署项目到tomcat后却无法访问
    Application context not configured this file
    tomcat体系结构
    MySql语法概述
    MySql数据库事务
    split(",")与split(",",-1)的区别
    Cookie,session的原理及用法
  • 原文地址:https://www.cnblogs.com/Saviorsyang/p/6308968.html
Copyright © 2020-2023  润新知