10.Python基础语法---06集合

集合

基本定义:通过{}构建

特性:无序,不可重复,元素可以是不同类型(不支持unhashable 类型的元素)

基础操作

  1. 集合运算
    1.1 |并集运算符:求两个集合并或合集
    1.2 &交集运算符:求两个集合交集
    1.3 -差集运算符:求两个集合差集

    s1 = {1, 2, 3, 4, 5, 6}
    s2 = {4, 5, 6, 7, 8, 9}
    print("s1: {}".format(s1))
    print("s2: {}".format(s2))
    # 并集
    s = s1 | s2
    print("s1|s2: {}".format(s))
    # 交集
    s = s1 & s2
    print("s1 & s2: {}".format(s))
    # 差集
    s = s1 - s2
    print("s1 - s2: {}".format(s))
    # 差集
    s = s2 - s1
    print("s2 - s1: {}".format(s))
    
    执行结果:
    s1: {1, 2, 3, 4, 5, 6}
    s2: {4, 5, 6, 7, 8, 9}
    s1|s2: {1, 2, 3, 4, 5, 6, 7, 8, 9}
    s1 & s2: {4, 5, 6}
    s1 - s2: {1, 2, 3}
    s2 - s1: {8, 9, 7}
    
    
  2. 集合操作
    集合只存在添加和删除两个操作,无所谓修改操作
    2.1 添加
    set.add(x)——将x添加到集合中
    2.2 删除
    set.remove(x)——从集合中移除元素 x。 如果 x 不存在于集合中则会引发 KeyError。
    set.discard(x)——如果元素 x 存在于集合中则将其移除。
    set.pop()——从集合中移除并返回任意一个元素。 如果集合为空则会引发 KeyError。任意代表随机吗?!
    set.clear()——清空集合

    # 添加
    s = {1,2,3,4,5}
    print("s:{}".format(s))
    s.add(6)
    print("s:{}".format(s))
    s.add(3)
    print("s:{}".format(s))
    
    print("*" * 23)
    
    # 删除
    # remove()
    s.remove(4)
    print("s:{}".format(s))
    
    # s.remove(4)
    # Traceback (most recent call last):
    #   File "XXXXXXX", line XXX, in     
    #     s.remove(4)
    # KeyError: 4
    
    # discard()
    returntest = s.discard(5)
    print("returntest:{}".format(returntest))
    print("s:{}".format(s))
    s.discard(5) # 不抛异常
    
    # pop()
    s.pop()
    print("s:{}".format(s))
    s.pop()
    print("s:{}".format(s))
    s.pop()
    print("s:{}".format(s))
    s.pop()
    print("s:{}".format(s))
    # s.pop()
    # print("s:{}".format(s))
    # Traceback (most recent call last):
    #   File "d:/Dev/WorkSpace/01SunnyLearn/Python/Basic/SunnyPython/basic/02group.py", line 620, in 
    #     s.pop()
    # KeyError: 'pop from an empty set'
    
    # All Test
    hello = "Hello Python World! We Are Family"
    for char in hello:
        s.add(char)
    
    print("s:{}".format(s))
    spop = s.pop()
    print("spop:{}".format(spop))
    print("s:{}".format(s))
    spop = s.pop()
    print("spop:{}".format(spop))
    print("s:{}".format(s))
    spop = s.pop()
    print("spop:{}".format(spop))
    print("s:{}".format(s))
    
    s.clear()
    print("s:{}".format(s))
    
    执行结果:
    s:{1, 2, 3, 4, 5}
    s:{1, 2, 3, 4, 5, 6}
    s:{1, 2, 3, 4, 5, 6}
    ***********************
    s:{1, 2, 3, 5, 6}
    returntest:None
    s:{1, 2, 3, 6}
    s:{2, 3, 6}
    s:{3, 6}
    s:{6}
    s:set()
    s:{'r', 'A', 'd', 'n', 'H', 't', 'm', ' ', 'h', 'P', 'l', 'e', 'W', 'F', 'a', 'y', '!', 'i', 'o'}
    spop:A
    s:{'r', 'd', 'n', 'H', 't', 'm', ' ', 'h', 'P', 'l', 'e', 'W', 'F', 'a', 'y', '!', 'i', 'o'}
    spop:d
    s:{'r', 'n', 'H', 't', 'm', ' ', 'h', 'P', 'l', 'e', 'W', 'F', 'a', 'y', '!', 'i', 'o'}
    spop:n
    s:{'r', 'H', 't', 'm', ' ', 'h', 'P', 'l', 'e', 'W', 'F', 'a', 'y', '!', 'i', 'o'}
    s:set()
    
  3. 其他操作
    3.1 len() 集合大小
    3.2 in, not in 成员判断符

    s = "Hello Python World! We are Family!"
    print("s:{}".format(s))
    print("s字符串列表长度:{}".format(len(s)))
    
    t = set()
    for char in s:
       t.add(char)
    print("t:{}".format(t))
    print("t大小:{}".format(len(t)))
    
    print("{}".format("H" in t))
    print("{}".format("H" not in t))
    print("{}".format("U" in t ))
    print("{}".format("U" not in  t ))
    
    执行结果:
    s:Hello Python World! We are Family!
    s字符串列表长度:34
    t:{'r', 'm', 'i', 'H', 'y', 'W', '!', 'F', ' ', 'd', 'a', 'o', 'l', 'P', 'h', 'e', 'n', 't'}
    t大小:18
    True
    False
    False
    True
    

补充

  • 如何定义空集合
    {}其实是定义了一个空字典;定义空集合需要使用set()
  • 集合无序性
    作为一种无序的多项集,集合并不记录元素位置或插入顺序。 相应地,集合不支持索引、切片或其他序列类的操作

QA

Q1:为什么集合不支持unhashable 类型的元素
A:
简单的可以理解为可哈希其实就是不可变,只有哈希不可变才能比较确认是否重复,才能实现集合的无重复特性哦!!!集合在内部需要使用哈希值来判断是否相等!!!

  • hashable -- 可哈希
    一个对象的哈希值如果在其生命周期内绝不改变,就被称为 可哈希 (它需要具有 __hash__() 方法),并可以同其他对象进行比较(它需要具有 __eq__() 方法)。可哈希对象必须具有相同的哈希值,比较结果才会相同。,

    可哈希性使得对象能够作为字典键或集合成员使用,因为这些数据结构要在内部使用哈希值。

    大多数 Python 中的不可变内置对象都是可哈希的;可变容器(例如列表或字典)都不可哈希;不可变容器(例如元组和 frozenset)仅当它们的元素均为可哈希时才是可哈希的。 用户定义类的实例对象默认是可哈希的。 它们在比较时一定不相同(除非是与自己比较),它们的哈希值的生成是基于它们的 id()

    内置类型分类
    hashable:int, float, decimal, complex, bool, str, tuple,
    unhashable: list, dict, set
    验证方法:

    >>> type(str.__hash__)
    
    >>> type(int.__hash__)
    
    >>> type(float.__hash__)
    
    >>> import decimal
    >>> type(decimal.__hash__)
    
    >>> type(complex.__hash__)
    
    >>> type(bool.__hash__)
    
    >>> type(tuple.__hash__)
    
    
    >>> type(list.__hash__)
    
    >>> type(dict.__hash__)
    
    >>> type(set.__hash__)
    
    
  • hash方法

>>> hash(0)
0
>>> hash(23)
23
>>> hash(-23)
-23
>>> hash(23.24)
553402322211282967
>>> hash(-23.24)
-553402322211282967
>>> hash('23')
-4826980359952576689
>>> hash(23+24j)
24000095
>>> hash(23-24j)
-24000049
>>> hash(True)
1
>>> hash(False)
0
>>> hash(None)
124074573
>>> hash([1,2,3,4,5])
Traceback (most recent call last):
  File "", line 1, in 
TypeError: unhashable type: 'list'
>>> hash({'key1':1, 'key2':2})
Traceback (most recent call last):
  File "", line 1, in 
TypeError: unhashable type: 'dict'
>>> hash((1,2,3,4,5))
8315274433719620810
>>> hash((1,2,3,[4,5]))
Traceback (most recent call last):
  File "", line 1, in 
TypeError: unhashable type: 'list'
>>> hash((1,2,3,(4,5)))
-325972594757234431
>>> hash({1,2,3})
Traceback (most recent call last):
  File "", line 1, in 
TypeError: unhashable type: 'set'

Q2:set.pop()所谓任意的机制与原理
A: //TODO
https://stackoverflow.com/questions/21017188/set-pop-isnt-random

参考:
互联网相关资料

你可能感兴趣的:(10.Python基础语法---06集合)