Python--集合

# int string list tuple dict bool float set

# 集合,天生去重

# 定义集合
s = set() # 空的集合
s1 = {'1', '2', '3', '3'}
print(s1)

lists = [1, 2, 3, 4, 5, 5, 6, 7, 8]
print(set(lists))

# 集合是无序的,所以不能用下标取值

# 添加值
s1.add('9')
print(s1)

# 交集
s2 = {'1', '2', '3'}
s3 = {'1', '2', '5'}
print(s2.intersection(s3))
print(s2 & s3)
# 并集
print(s2.union(s3))
print(s2 | s3)
# 差集
print(s2.difference(s3)) # s2里面谁没在s3里面
print(s2 - s3)

# 删除值
s3.remove('5')
s3.pop() # 随机删除一个值
posted @ 2018-01-09 14:38  王思磊  阅读(119)  评论(0编辑  收藏  举报