python之集合set
阿新 • • 發佈:2017-06-23
所有 copy clas 集合 ear hab allow span -s
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‘]))
python之集合set