集合
无序的不可随机访问的,,不可重复的元素集合
set为可变集合
forzenset为不可变集合
可变集合
s = {1, 2, 3}
s2 = {‘abc’}
print(s2)
s = set(x for x in range(1,10))
print(s)
fs = frozenset(‘abc’)
print(fs)
s = frozenset(x**2 for x in range(1,10) if x % 2 == 0)
print(s)
增
s = {1,2,3}
s.add(4)
print(s)
删
s.remove(3)
print(s)
s.discard(2)
print(s)
s.pop()
print(s)
print(s.clear())
集合之间操作
交集
s1 = {1,2,3,4,5}
s2 = {4,5,6,7}
result = s1.intersection(s2)
print(result)
result = s1 & s2
print(result)
s1.intersection(s2)
print(s1)
并集
resul