码迷,mamicode.com
首页 > 编程语言 > 详细

set的特性和基本用法——python3.6

时间:2017-11-17 00:15:27      阅读:322      评论:0      收藏:0      [点我收藏+]

标签:car   view   随机   type   data   直接   差集   div   upd   

 

  • 特性
    • 无序,不重复的数据组合,用{}表示,eg:{1,2,3,4,5,6}
  • 用途
    • 去重,把一个列表变成集合,就自动去重了
    • 关系测试,测试两组数据之间的交集,差集,并集,对称差集,包含(子集和超集,相交和不相交)关系
  • 基本语法
    • set():创建一个空集合,用set()创建,不能直接用s = {},这是创建一个空字典
    • 技术分享
      s = set()
      print(type(s))    # =><class set>
      View Code
    • add:往集合中添加一个元素,如果添加的元素集合中已有,则集合没有任何变化,只能添加不可变数据
    • 技术分享
      s = {1,2,3,4,5}
      s.add(1)
      print(s)    # =>{1, 2, 3, 4, 5}
      s.add(6)
      print(s)    # =>{1, 2, 3, 4, 5, 6}
      View Code
    • update:往集合中添加多个元素,自动去重
    • 技术分享
      s = {1,2,3,4,5}
      s.update({1,2,6,7,8})
      print(s)    # =>{1, 2, 3, 4, 5, 6, 7, 8}
      View Code
    • discard:删除指定元素,如果要删除的元素集合中不存在,do nothing
    • 技术分享
      s = {1,2,3,4,5}
      s.discard(1)
      print(s)    # =>{2, 3, 4, 5}
      View Code
    • remove:删除指定元素,如果要删除的元素集合中不存在,报错
    • 技术分享
      s = {1,2,3,4,5}
      s.remove(1)
      print(s)    # =>{2, 3, 4, 5}
      s.remove(6) # =>报错
      View Code
    • pop:随机删除一个元素,无序的,当集合为空时,报错
    • 技术分享
      s = {1,2}
      s.pop()
      print(s)    # =>{2}
      s.pop()
      s.pop()     # =>报错
      View Code
    • clear:清空
    • 技术分享
      s = {1,2,3,4,5}
      s.clear()
      print(s)    # =>set()
      View Code
    • copy:浅复制
    • 技术分享
      s = {1,2,3,4,5}
      s1 = s.copy()
      print(s1)   # =>{1,2, 3, 4, 5}
      View Code
    • difference:差集,差集后的结果是一个新的集合

    • 技术分享
      s1 = {1,2,3,4,5}
      s2 = {4,5,6,7,8}
      s3 = s1.difference(s2)
      print(s3)   # =>{1, 2, 3}
      s3 = s1-s2
      print(s3)   # =>{1, 2, 3}
      s4 = s2.difference(s1)
      print(s4)   # =>{8, 6, 7}
      s4 = s2-s1
      print(s4)   # =>{8, 6, 7}
      View Code
    • difference_update:删除s1集合中含有s2集合中的元素,并返回给s1

    • 技术分享
      s1 = {1,2,3,4,5}
      s2 = {4,5,6,7,8}
      s3 = s1.difference_update(s2)
      print(s1)   # =>{1, 2, 3}
      View Code
    • intersection:交集,交集后的结果是一个新的集合

    • 技术分享
      s1 = {1,2,3,4,5}
      s2 = {4,5,6,7,8}
      s3 = s1.intersection(s2)
      print(s3)   # =>{4, 5}
      s3 = s1 & s2
      print(s3)   # =>{4, 5}
      s3 = s2.intersection(s1)
      print(s3)   # =>{4, 5}
      s3 = s2 & s1
      print(s3)   # =>{4, 5}
      View Code
    • intersection_update:将s1和s2的交集更新给s1

    • 技术分享
      s1 = {1,2,3,4,5}
      s2 = {4,5,6,7,8}
      s1.intersection_update(s2)
      print(s1)   # =>{4, 5}
      View Code
    •  union:并集

    • 技术分享
      s1 = {1,2,3,4,5}
      s2 = {4,5,6,7,8}
      s3 = s1.union(s2)
      print(s3)   # =>{1, 2, 3, 4, 5, 6, 7, 8}
      s3 = s1 | s2
      print(s3)   # =>{1, 2, 3, 4, 5, 6, 7, 8}
      View Code
    • isdisjoint:判断两个集合是否有交集,如果没有,返回True,如果有,返回False

    • 技术分享
      s1 = {1,2,3,4,5}
      s2 = {4,5,6,7,8}
      s3 = {8,9}
      print(s1.isdisjoint(s2))   # => False
      print(s1.isdisjoint(s3))   # =>True
      View Code
    • issubset:判断s2是否是s1的子集,如果是,则True,or,False

      技术分享
      s1 = {1,2,3,4,5}
      s2 = {4,5}
      print(s2.issubset(s1))   # =>True
      View Code
    • issupper:判断s1是否是s2的超集
    • 技术分享
      s1 = {1,2,3,4,5}
      s2 = {4,5}
      print(s1.issuperset(s2))   # =>True
      View Code
    • symmetric_difference:对称差集
    • 技术分享
      s1 = {1,2,3,4,5}
      s2 = {4,5,6,7,8}
      s3 = s1.symmetric_difference(s2)
      print(s3)   # =>{1, 2, 3, 6, 7, 8}
      s3 = s2.symmetric_difference(s1)
      print(s3)   # =>{1, 2, 3, 6, 7, 8}
      View Code
    • symmetric_difference_updata:将对称差集更新给s1,或者s2
    • 技术分享
      s1 = {1,2,3,4,5}
      s2 = {4,5,6,7,8}
      s1.symmetric_difference_update(s2)
      print(s1)   # =>{1, 2, 3, 6, 7, 8}
      s2.symmetric_difference_update(s1)
      print(s2)   # =>{1, 2, 3, 4, 5}
      View Code

       

  • 其他用法:
    • in ,not in 判断某元素是否在集合内
    • == ,!=判断两个集合是否相等

set的特性和基本用法——python3.6

标签:car   view   随机   type   data   直接   差集   div   upd   

原文地址:http://www.cnblogs.com/GraceZ/p/7846603.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!