1,集合Set
(1)集合的創建:
>>> a ={1,2,3,'4'}
>>> type(a)
<type 'set'>
>>> b = set([1,2,3,4])
>>> type(b)
<type 'set'>
>>> c = set('helloworld')
>>> c
set(['e', 'd', 'h', 'l', 'o', 'r', 'w'])
(2)集合的增刪改除
>>> a
set([1, 2, 3, 4])
>>> a.add('5') #增加
>>> a
set([1, 2, 3, 4, '5'])
>>> a.remove(1) #刪除特定元素
>>> a
set([2, 3, 4, '5'])
>>> >>> a.update('678') #增加
>>> a
set([2, 3, 4, '5', '7', '6', '8'])
>>> a -= set('57') #減去集合
>>> a
set([2, 3, 4, '6', '8'])
2,不可變集合frozenset
(1)不可變集合的創建:
>>> a1=frozenset(['12','13',2,3])
>>> a1
frozenset([3, '13', '12', 2])
(2) 不可變集合不可修正
>>> a1.add('1')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'frozenset' object has no attribute 'add'