美文网首页
Python基础 - 集合 Set

Python基础 - 集合 Set

作者: 彼岸的渔夫 | 来源:发表于2018-05-21 15:12 被阅读94次
    人生苦短,我用Python:花了一生学Python

    A set object is an unordered collection of distinct hashable objects. Common uses include membership testing, removing duplicates from a sequence, and computing mathematical operations such as intersection, union, difference, and symmetric difference.

    set对象是一个无序不重复元素集, 基本功能包括关系测试和消除重复元素. 集合对象还支持union(并), intersection(交), difference(差)和sysmmetric difference(对称差集)等数学运算.

    Like other collections, sets support x in set, len(set), and for x in set. Being an unordered collection, sets do not record element position or order of insertion. Accordingly, sets do not support indexing, slicing, or other sequence-like behavior.

    sets支持x in set, len(set),和 for x in set。作为一个无序的集合,sets不记录元素位置或者插入顺序。因此,sets不支持 indexing(索引), slicing(切片), 或其它类序列(sequence-like)的操作。

    有两种内置set类型:set and frozensetset 类型是可变的,frozenset类型是不可变的。

    • set无序排序且不重复,是可变的,有add()remove()等方法。既然是可变的,所以它不存在哈希值。
    • frozenset是冻结的集合,它是不可变的,存在哈希值,好处是它可以作为字典的key,也可以作为其它集合的元素。缺点是一旦创建便不能更改,没有add()remove()方法。

    创建集合

    • 非空集合sets (非 frozensets) 可以通过 用逗号隔开的元素放在大括号中创建
    • set()函数用于创建集合
    >>> s = {'a','b','c','d'}
    >>> s
    {'a', 'd', 'b', 'c'}
    >>> type(s)
    <class 'set'>
    
    >>> s2 = {['a','b','c','d']}    #  列表
    Traceback (most recent call last):
      File "<pyshell#3>", line 1, in <module>
        s2 = {['a','b','c','d']}
    TypeError: unhashable type: 'list'
    
    >>> s3 = {('a','b','c','d')}   #  元组
    >>> s3
    {('a', 'b', 'c', 'd')}
    >>> type(s3)
    <class 'set'>
    
    >>> s = set()    #注意在创建空集合的时候只能使用s=set(),因为s={}创建的是空字典
    >>> s
    set()
    >>> s = set([2,3,4,5,6])
    >>> s
    {2, 3, 4, 5, 6}
    
    ''' set() 函数'''
    >>> a = set('hello')
    >>> b = set('boy')
    >>> c = set({'k1':'v1','k2':'v2'})
    >>> a
    {'h', 'e', 'l', 'o'}
    >>> b
    {'y', 'b', 'o'}
    >>> c
    {'k1', 'k2'}
    >>> a,b,c
    ({'h', 'e', 'l', 'o'}, {'y', 'b', 'o'}, {'k1', 'k2'})
    

    集合的基本操作

    以下方法对setfrozenset均适用:

    • len()

    • x in s

    • x not in s

    • issubset(other)
      判断是否为子集。

    • issuperset()
      判断是否为超集。

    • union(*others)
      s | t
      并集操作:返回两个集合所有的元素的新集合。

    • intersection(*others)
      s & t
      交集操作:返回s和t集合共有的元素新集合。

    • difference(*others)
      s - t
      差分操作: s 中的元素,而不是 t 中的元素

    • symmetric_difference(other)
      s ^ t
      对称差分操作:s 或 t 中的元素,但不是 s 和 t 共有的元素。

    • copy()
      浅复制

    注:非操作符的方法union(), intersection(), difference(),和 issubset()
    issuperset()symmetric_difference()接受任何可迭代对象作为参数 。

    以下方法适用于可变的set,但不适用于不可变的frozenset:

    • update(*others)
      s.update(t) , s |= t (Union)
      更新集合: 将 t 中的成员添加 s
    >>> s = {'a','b','c','d'}
    >>> t = {'a','11','22'}
    >>> s.update(t)
    >>> s
    {'22', 'b', 'a', 'd', '11', 'c'}
    
    • intersection_update(*others)
      s &= t
      交集更新: s 中仅包括 s 和 t 中共有的成员

    • difference_update(*others)
      s -= t
      差集更新: s 中包括仅属于 s 但不属于 t 的成员

    • symmetric_difference_update(other)
      s ^= t
      对称差分更新: s 中包括仅属于 s 或仅属于 t 的成员

    • add(elem)
      添加一个元素到集合中。
    >>> s = {'a','b','c','d'}
    >>> s.add('e')
    >>> s
    {'a', 'e', 'c', 'b', 'd'}
    
    • remove(elem)
      从集合中删除一个元素。如果该元素不存在,将会抛出KeyError。·
    >>> s = {'a','b','c','d'}
    >>> s.remove('a')
    >>> s
    {'d', 'b', 'c'}
    >>> s.remove('e')
    Traceback (most recent call last):
      File "<pyshell#38>", line 1, in <module>
        s.remove('e')
    KeyError: 'e'
    
    • discard(elem)
      从集合中删除元素,如果该元素存在的话。“如果存在就删除,不存在不会报错”
    >>> s = {'a','b','c','d'}
    >>> s.discard('a')
    >>> s
    {'d', 'b', 'c'}
    >>> s.discard('e')   # 移除不存的元素不会报错
    >>> s
    {'d', 'b', 'c'}
    
    • pop()
      删除并返回集合的任意一个元素。 如果集合是空集合,将抛出KeyError
    >>> s = {'a','b','c','d'}
    >>> s.pop()
    'a'
    >>> s.pop()
    'd'
    >>> s
    {'b', 'c'}
    >>> s.clear()
    >>> s.pop()  
    Traceback (most recent call last):
      File "<pyshell#29>", line 1, in <module>
        s.pop()
    KeyError: 'pop from an empty set'
    
    • clear()
      清空集合的所有元素。
    >>> s = {'a','b','c','d'}
    >>> s.clear()
    >>> s
    set()
    

    注:非操作符的方法update(), intersection_update(), difference_update()symmetric_difference_update() 接受任何可迭代对象作为参数。

    相关文章

      网友评论

          本文标题:Python基础 - 集合 Set

          本文链接:https://www.haomeiwen.com/subject/fflgrftx.html