6-3 集合set和frozenset

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)

 in ()
      1 s = frozenset("abcddeeffd")
      2 print(s)
----> 3 s.add("H")


AttributeError: 'frozenset' object has no attribute 'add'

frozenset是不可以修改的

你可能感兴趣的:(6-3 集合set和frozenset)