435421 发表于 2016-4-25 09:25:01

python 集合set


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# -*- coding: utf-8 -*-

#无序不重复

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

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'




页: [1]
查看完整版本: python 集合set