6-3 集合set和frozenset
2018-08-26 本文已影响0人
正在努力ing
l = ['span','span','eggs','span']
set(l)
{'eggs', 'span'}
set集合是唯一的,同时set集合还实现了交集,合集,差集等运算
m = ['spam','zhou']
mset = set(m)
lset = set(l)
m
['spam', 'zhou']
l
['span', 'span', 'eggs', 'span']
mset
{'spam', 'zhou'}
mset | lset
{'eggs', 'spam', 'span', 'zhou'}
mset & lset
set()
s = frozenset("abcddeeffd")
print(s)
s.add("H")
frozenset({'c', 'd', 'a', 'e', 'f', 'b'})
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-3-ec8be88b9c56> in <module>()
1 s = frozenset("abcddeeffd")
2 print(s)
----> 3 s.add("H")
AttributeError: 'frozenset' object has no attribute 'add'
frozenset是不可以修改的