zoukankan      html  css  js  c++  java
  • 集合<class'set'>

    >>> s = {1,2,3,4}

    >>> s&{1,3}
    {1, 3}
    >>> s|{11}
    {1, 2, 3, 4, 11}
    >>> s>{1,3}
    True
    >>> s.union({1,3})
    {1, 2, 3, 4}
    >>> s.union((11,22))
    {1, 2, 3, 4, 11, 22}
    >>> s.union([111,22])
    {1, 2, 3, 4, 111, 22}
    >>> s.intersection((3,4,5,6))
    {3, 4}
    >>> s.issubset(range(1,111))
    True
    >>> s.add((12,13))      #no list or dict,but tuple only
    >>> s
    {1, 2, 3, 4, (12, 13)}
    >>> s.add({112,122})
    Traceback (most recent call last):
    File "<pyshell#95>", line 1, in <module>
    s.add({112,122})
    TypeError: unhashable type: 'set'
    >>> help(s.add)
    Help on built-in function add:

    add(...) method of builtins.set instance
    Add an element to a set.

    This has no effect if the element is already present.

    >>> s.add(0)
    >>> s
    {0, 1, 2, 3, 4, (12, 13)}
    >>>{x for x in 'spam'}

    {'m', 'p', 'a', 's'}

    >>> {x*2 for x in 'spam'}
    {'ss', 'pp', 'mm', 'aa'}

    >>> engineers = {'Bob', 'Sue', 'Ann', 'Vic'}
    >>> managers = {'Tom', 'Sue'}
    >>> managers ^ engineers    #Who is in one but not both?
    {'Ann', 'Vic', 'Bob', 'Tom'}
    >>> managers & engineers    #Who is in both
    {'Sue'}

    >>> engineers - managers   #engineers who are not managers
    {'Ann', 'Vic', 'Bob'}
    >>> managers - engineers   #managers who are not engineers
    {'Tom'}

    >>> (managers | engineers) - (managers ^ engineers)   #Intersecion! Same as &
    {'Sue'}

  • 相关阅读:
    数组中寻找和为X的两个元素
    JSP&Servlet学习笔记(一)
    自下而上的动态规划算法
    计数排序
    快速排序
    堆排序
    LeetCode-001题解
    算法不归路之最大子序列(C++版)
    算法不归路之插入排序(C版)
    互联网五层模型
  • 原文地址:https://www.cnblogs.com/wawawawa-briefnote/p/8677725.html
Copyright © 2011-2022 走看看