python之集合set
1.测试
1 # python2和python3方法列表相同 2 ops23 = ['add', 'clear', 'copy', 'difference', 'difference_update', 'discard', 'intersection', 'intersection_update', 3 'isdisjoint', 'issubset', 'issuperset', 'pop', 'remove', 'symmetric_difference', 4 'symmetric_difference_update', 'union', 'update'] 5 6 for op in ops23: 7 try: 8 print('#'*10) 9 S_abc = set('abc') 10 S_cde = set('cde') 11 print(op,eval('S_abc.{}(S_cde)'.format(op))) 12 print('S_abc:',S_abc) 13 print('S_cde:',S_cde) 14 except Exception as why: 15 print(op,why)
2. 总结
(1)元素操作
S.add 添加hashable type,注意set为unhashable type
S.pop随机删除并返回,remove删除指定元素可能报错,discard删除指定元素不会报错
S.clear 清空
S.copy 浅复制shallow copy
(2)判断关系
S.isdisjoint不相交 S.issubset子集 S.issuperset超集
(3)集合操作
命名方法 | 运算符语法 | 图例结果 | 对应update |
S.difference(S1) | S-S1 | A | S -=S1 |
S.intersection(S1) | S&S1 | B | S &=S1 |
S.symmetric_difference(S1) | S^S1 | A+C | S ^=S1 |
S.union(S1) | S|S1 | A+B+C | S |=S1 |
(a) S.xxx返回结果集合,S.xxx_update原地更新S,返回None,union对应方法为update而不是union_update
(b)命名方法相比运算符语法,S1可以是由可哈希的项目组成的任意迭代
>>> set('abc').union('cde')
set(['a', 'c', 'b', 'e', 'd'])
>>> set('abc').union(set('cde'))
set(['a', 'c', 'b', 'e', 'd'])
>>>
3.输出分析
1 ########## 2 ('add', TypeError("unhashable type: 'set'",)) 3 ########## 4 ('clear', TypeError('clear() takes no arguments (1 given)',)) 5 ########## 6 ('copy', TypeError('copy() takes no arguments (1 given)',))
Return a shallow copy of a set.
In [45]: S_abc.copy() == S_abc
Out[45]: True
In [46]: S_abc.copy() is S_abc
Out[46]: False
7 ########## 8 ('difference', set(['a', 'b'])) 9 ('S_abc:', set(['a', 'c', 'b'])) 10 ('S_cde:', set(['c', 'e', 'd'])) 11 ########## 12 ('difference_update', None) 13 ('S_abc:', set(['a', 'b'])) #更新S1,留不同 14 ('S_cde:', set(['c', 'e', 'd']))
15 ########## 16 ('discard', None) 17 ('S_abc:', set(['a', 'c', 'b'])) 18 ('S_cde:', set(['c', 'e', 'd']))
19 ########## 20 ('intersection', set(['c'])) 21 ('S_abc:', set(['a', 'c', 'b'])) 22 ('S_cde:', set(['c', 'e', 'd'])) 23 ########## 24 ('intersection_update', None) 25 ('S_abc:', set(['c'])) #更新S1,留交集 26 ('S_cde:', set(['c', 'e', 'd']))
27 ########## 28 ('isdisjoint', False) 29 ('S_abc:', set(['a', 'c', 'b'])) 30 ('S_cde:', set(['c', 'e', 'd']))
Return True if two sets have a null intersection. 31 ########## 32 ('issubset', False) 33 ('S_abc:', set(['a', 'c', 'b'])) 34 ('S_cde:', set(['c', 'e', 'd'])) 35 ########## 36 ('issuperset', False) 37 ('S_abc:', set(['a', 'c', 'b'])) 38 ('S_cde:', set(['c', 'e', 'd']))
39 ########## 40 ('pop', TypeError('pop() takes no arguments (1 given)',))
Remove and return an arbitrary set element.随机返回 41 ########## 42 ('remove', KeyError(set(['c', 'e', 'd']),)) 43 ##########
44 ('symmetric_difference', set(['a', 'b', 'e', 'd'])) 45 ('S_abc:', set(['a', 'c', 'b'])) 46 ('S_cde:', set(['c', 'e', 'd'])) 47 ########## 48 ('symmetric_difference_update', None) 49 ('S_abc:', set(['a', 'b', 'e', 'd'])) #更新S1,所有异或 01 10 50 ('S_cde:', set(['c', 'e', 'd']))
51 ########## 52 ('union', set(['a', 'c', 'b', 'e', 'd'])) #并集 53 ('S_abc:', set(['a', 'c', 'b'])) 54 ('S_cde:', set(['c', 'e', 'd'])) 55 ########## 56 ('update', None) #更新S1为全集 57 ('S_abc:', set(['a', 'c', 'b', 'e', 'd'])) 58 ('S_cde:', set(['c', 'e', 'd']))