49

7.2集合类型操作符-小嘉欣的博客-51CTO博客

 6 years ago
source link: http://blog.51cto.com/10777193/2072641
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

集合类型操作符

  1. In not in
  2. 集合等价与不等价(== ,!=)

    set1=set("jiaxin")
    set2=set("jiaxinxinxin")
    print(set1==set2)
    # True
    print(set1<set("jiaxin123"))
    #True
    print(set1<set("jiaxin"))   #包含关系必须超集比子集大
    # False
  3. 子集,超集
    set1=set("jiaxin")
    set2=set("jiaxinxinxin")
    print(set1==set2)
    # True
    print(set1<set("jiaxin123"))
    #True
    print(set1<set("jiaxin"))   #包含关系必须超集比子集大
    # False
  4. 并集:联合(l)union and |

    联合(union)操作与集合or操作其实是等价的,联合符号有个等价的方法,union()
    #取并集 :and  union()  | 管道符
    print(set("jiaxin") and set("jiaxin1"))  #and 取并集
    # {'i', 'a', 'n', '1', 'x', 'j'}
    print(set("jiaxin").union(set("jiaxin1")))
  5. 交集(&)

    #取交集:or intersection()  &管道符
    print(set("jiaxin") or set("jiaxin01"))  #or 取交集
    # {'i', 'a', 'n', 'x', 'j'}
    print(set("jiaxin").intersection(set("jiaxin01")))
  6. 差集(- 或^)

    #取差集,前者有,后者没有的数据:difference()  - 管道符
    print(set("jiaxin01").difference(set("jiaxin")))
    #{'0', '1'}
    #对称差集,你有我没有,我有你没有的数据 symmetric_difference() ^ 管道符
    print(set("123").symmetric_difference(set("3456")))
    # {'2', '5', '1', '6', '4'}
  7. 父集和子集
    #父集和子集
    print(set("123").issuperset(set("1")))
    print(set("1").issubset(set("123")))
    # True
    # True

大家对内容有任何问题,欢迎留言,定在第一时间解答,谢谢大家!


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK