集合中,设A、B两个集合,有全部属于集合 A 且 属于集合B的元素所组成的集合,叫作集合A与集合B 的交集(intersection)python
In [1]: s1 = {1, 2, 3} In [2]: s2 = {2, 3, 4} In [3]: s1.intersection(s2) # 返回结果集, 原地不作修改的函数,交集是有一个交换率的 Out[3]: {2, 3} In [4]: s1 Out[4]: {1, 2, 3} In [5]: s2 Out[5]: {2, 3, 4} In [6]: s2.intersection(s1) Out[6]: {2, 3} In [7]:
In [8]: s1.intersection_update(s2) # 原地修改,并返回None In [9]: s1 Out[9]: {2, 3} In [10]: s2 Out[10]: {2, 3, 4} In [15]: s1 = {1, 2, 3} In [16]: s2 Out[16]: {2, 3, 4} In [17]: s1 & s2 # 交集运算重载符 Out[17]: {2, 3} In [18]: s2 & s1 Out[18]: {2, 3} In [19]:
集合A 和 集合B,当集合C中的元素仅仅存在于A中,但不存在于B中,而且A中存在、B中不存在的元素,所有存在于C中,那么C是A的差集api
In [22]: s1 Out[22]: {1, 2, 3} In [23]: s2 Out[23]: {2, 3, 4} In [24]: s1.difference(s2) Out[24]: {1} In [25]: s2.difference(s1) Out[25]: {4}
差集 是不具有交换率的bash
In [27]: s1.difference_update(s2) In [28]: s1 Out[28]: {1} In [29]: s1 = {1, 2, 3} In [30]: s1 - s2 # - 是差集的运算重载符 Out[30]: {1} In [31]: s2 - s1 Out[31]: {4} In [32]:
集合A 与 集合B:一个元素,要么在A中,要么在B中函数
In [32]: s1 Out[32]: {1, 2, 3} In [33]: s2 Out[33]: {2, 3, 4} In [34]: s1.symmetric_difference(s2) Out[34]: {1, 4} In [35]: s2.symmetric_difference(s1) Out[35]: {1, 4}
对称差集也是有交换率的测试
In [36]: s1.symmetric_difference_update(s2) In [37]: s1 Out[37]: {1, 4} In [38]: s2 Out[38]: {2, 3, 4} In [39]: s1.symmetric_difference_update(s2) In [40]: s1 Out[40]: {1, 2, 3} In [41]: s2 Out[41]: {2, 3, 4} In [42]: s1 ^ s2 # 对称差集的运算重载符 Out[42]: {1, 4} In [43]:
若集合A 和 集合B,则A和B的并集是:全部A中的元素 和 全部B中的元素,而没有其余元素的集合。ui
In [43]: s1.union(s2) Out[43]: {1, 2, 3, 4} In [44]: s1 Out[44]: {1, 2, 3} In [45]: s2 Out[45]: {2, 3, 4} In [46]: s2.union(s1) Out[46]: {1, 2, 3, 4} In [47]:
并集也具备交换率。spa
In [1]: s1 = {1, 2, 3} In [2]: s2 = {2, 3, 4} In [3]: s1.update(s2) In [4]: s1 Out[4]: {1, 2, 3, 4} In [5]: s1 = {1, 2, 3} In [6]: s1.update(s2) In [7]: s1 Out[7]: {1, 2, 3, 4} In [8]: s1 | s2 Out[8]: {1, 2, 3, 4} In [9]: s1 Out[9]: {1, 2, 3, 4} In [10]: s1 = {1, 2, 3} In [11]: s1 | s2 # | 是 并集的运算重载符 Out[11]: {1, 2, 3, 4}
补集通常指绝对补集,设 S 是一个集合,A是S的一个子集,由S中全部不属于A的元素 组成的集合,叫作子集A 在S中的绝对补集(简称补集 或 余集)3d
在程序语言中,没有补集运算code
由于在程序中,没法定义出绝对全集,因此没法求出绝对补集ip
In [12]: s1 = {1, 2, 3, 4} In [13]: s2 = {2, 3}
若是一个集合s2,每个元素都在集s1中,且集合s1 可能包含s2 中没有的元素,则集合s1 就是s2的一个超集,s2 就是 s1的一个子集。
In [12]: s1 = {1, 2, 3, 4} In [13]: s2 = {2, 3} In [14]: In [14]: s2.issubset(s1) Out[14]: True In [15]: s1.issubset(s2) Out[15]: False In [16]: s1.issuperset(s2) Out[16]: True In [17]: s2.issuperset(s1) Out[17]: False In [18]: def _issubest(s1, s2): ...: for x in s1: ...: if x not in s2: ...: return False ...: return True ...: In [19]: s1 Out[19]: {1, 2, 3, 4} In [20]: s2 Out[20]: {2, 3} In [21]: _issubest(s1, s2) Out[21]: False In [22]: _issubest(s2, s1) Out[22]: True In [23]: In [23]: def _issuperset(s1, s2): ...: for x in s2: ...: if x not in s1: ...: return False ...: return True ...: In [24]: _issuperset(s1, s2) Out[24]: True
In [25]: s1 Out[25]: {1, 2, 3, 4} In [26]: s2 Out[26]: {2, 3} In [27]: s1.isdisjoint(s2) Out[27]: False In [28]: s3 = {1, 2} In [29]: s4 = {3, 4} In [30]: s3.isdisjoint(s4) Out[30]: True
isdisjoint 判断两个集合 是否有交集, 若是有交集 返回False, 若是没有交集 返回True。
有一个API,须要经过权限认证,而且 须要有必定的权限才能够访问
例如:要求知足权限A,B,C中任意一项,有一个用户具备权限B,C,D 那么此用户是否有权限访问API呢?
如何判断?
In [31]: s1 = {'A', 'B', 'C'} In [32]: s2 = {'B', 'C', 'D'} In [33]: if not s1.isdisjoint(s2): ...: print('get api success') ...: get api success In [34]:
有一个任务列表, 存储所有的人物,有一个列表,存储已经完成的任务,找出未完成的任务
怎么办?
集合的元素 必须可 hash
In [34]: hash(b'abc') Out[34]: -4664515049651371347 In [35]: hash(1) # 测试是否可哈希 Out[35]: 1 In [36]: hash('a') Out[36]: -4794389817845320130 In [37]: hash([1, 2]) --------------------------------------------------------------------------- TypeError Traceback (most recent call last) <ipython-input-37-4b420d0158ba> in <module>() ----> 1 hash([1, 2]) TypeError: unhashable type: 'list' In [38]: hash({1, 2}) --------------------------------------------------------------------------- TypeError Traceback (most recent call last) <ipython-input-38-42051ea12b27> in <module>() ----> 1 hash({1, 2}) TypeError: unhashable type: 'set' In [39]: hash(bytearray(b'abc')) --------------------------------------------------------------------------- TypeError Traceback (most recent call last) <ipython-input-39-3d8516aba17b> in <module>() ----> 1 hash(bytearray(b'abc')) TypeError: unhashable type: 'bytearray' In [40]:
In [40]: a = 1 In [41]: a.__hash__() Out[41]: 1 In [42]: l = [1, 2] In [43]: l.__hash__() --------------------------------------------------------------------------- TypeError Traceback (most recent call last) <ipython-input-43-43badc6ed4b1> in <module>() ----> 1 l.__hash__() TypeError: 'NoneType' object is not callable In [44]: help(hash) Help on built-in function hash in module builtins: hash(obj, /) Return the hash value for the given object. Two objects that compare equal must also have the same hash value, but the reverse is not necessarily true. ~ (END)