python 集合set

# -*- coding: utf-8 -*-

#無序不重複

set1 = {}
str = 'dsfhadfhhjdsfewhiufewhfewf'
set2 ={1,2,3,4,5,6,7,89,9,9,9,9}
print(set2) #set([1, 2, 3, 4, 5, 6, 7, 9, 89])

set3 = set(str)
print(set3) #set(['a', 'e', 'd', 'f', 'i', 'h', 'j', 's', 'u', 'w'])

print('a' in set3)  #True
print(len(set3))  #10
set3.add('hello')
print(set3) #set(['a', 'e', 'd', 'f', 'i', 'h', 'j', 's', 'u', 'w', 'hello'])
set3.remove('a')
print(set3) #set(['e', 'd', 'f', 'i', 'h', 'j', 's', 'u', 'w', 'hello'])

#不可變集合
set4 = frozenset(str)
#set4.add(0) #AttributeError: 'frozenset' object has no attribute 'add'


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章