python之集合set

时间:2021-07-08 07:15:05

1.测试

 # python2和python3方法列表相同
ops23 = ['add', 'clear', 'copy', 'difference', 'difference_update', 'discard', 'intersection', 'intersection_update',
'isdisjoint', 'issubset', 'issuperset', 'pop', 'remove', 'symmetric_difference',
'symmetric_difference_update', 'union', 'update'] for op in ops23:
try:
print('#'*10)
S_abc = set('abc')
S_cde = set('cde')
print(op,eval('S_abc.{}(S_cde)'.format(op)))
print('S_abc:',S_abc)
print('S_cde:',S_cde)
except Exception as why:
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)集合操作

python之集合set

命名方法 运算符语法 图例结果 对应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.输出分析

 ##########
('add', TypeError("unhashable type: 'set'",))
##########
('clear', TypeError('clear() takes no arguments (1 given)',))
##########
('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 ##########
('difference', set(['a', 'b']))
('S_abc:', set(['a', 'c', 'b']))
('S_cde:', set(['c', 'e', 'd']))
##########
('difference_update', None)
('S_abc:', set(['a', 'b'])) #更新S1,留不同
('S_cde:', set(['c', 'e', 'd'])) ##########
('discard', None)
('S_abc:', set(['a', 'c', 'b']))
('S_cde:', set(['c', 'e', 'd'])) ##########
('intersection', set(['c']))
('S_abc:', set(['a', 'c', 'b']))
('S_cde:', set(['c', 'e', 'd']))
##########
('intersection_update', None)
('S_abc:', set(['c'])) #更新S1,留交集
('S_cde:', set(['c', 'e', 'd'])) ##########
('isdisjoint', False)
('S_abc:', set(['a', 'c', 'b']))
('S_cde:', set(['c', 'e', 'd']))
Return True if two sets have a null intersection.
##########
('issubset', False)
('S_abc:', set(['a', 'c', 'b']))
('S_cde:', set(['c', 'e', 'd']))
##########
('issuperset', False)
('S_abc:', set(['a', 'c', 'b']))
('S_cde:', set(['c', 'e', 'd'])) ##########
('pop', TypeError('pop() takes no arguments (1 given)',))
Remove and return an arbitrary set element.随机返回
##########
('remove', KeyError(set(['c', 'e', 'd']),))
########## ('symmetric_difference', set(['a', 'b', 'e', 'd']))
('S_abc:', set(['a', 'c', 'b']))
('S_cde:', set(['c', 'e', 'd']))
##########
('symmetric_difference_update', None)
('S_abc:', set(['a', 'b', 'e', 'd'])) #更新S1,所有异或 01 10
('S_cde:', set(['c', 'e', 'd'])) ##########
('union', set(['a', 'c', 'b', 'e', 'd'])) #并集
('S_abc:', set(['a', 'c', 'b']))
('S_cde:', set(['c', 'e', 'd']))
##########
('update', None) #更新S1为全集
('S_abc:', set(['a', 'c', 'b', 'e', 'd']))
('S_cde:', set(['c', 'e', 'd']))