# 集合
# 1.创建集合
# 使用{}或者set(),但是创建空集合只能使用set(),因为{}用来创建字典
# 创建有数据的集合
# s1 = {10, 20, 30, 40, 50}
# print(f's1={s1}')
# # 集合没有顺序,不支持下标
#
# s2 = {10, 20, 30, 20, 40, 50}
# print(f's2={s2}')
#
# s3 = set('abcdefg')
# print(f's3={s3}')
# 2.创建空集合
# s4 = set()
# print(s4)
# print(type(s4))
# 2.集合的增加 add(),update()
# 2.1 add() 只能用来追加单个数据
# jihe1 = {10, 20, 30}
# jihe1.add(50)
# jihe1.add(10)
# print(jihe1)
# # 2.2 update() 增加的是数据序列
# jihe1.update([10, 40, 60])
# print(jihe1)
# 3. 集合的删除
# 3.1 remove() 删除集合中的指定数据,如果数据不存在则报错
# jihe2 = {10, 20, 30}
# jihe2.remove(10)
# print(jihe2)
#
# # 3.2 discard() 删除集合中的指定数据,如果数据不存在不报错
# jihe2.discard(40)
# print(jihe2)
#
# # 3.3 pop() 随机删除某个数据,并返回这个数据
# del_num = jihe2.pop()
# print(del_num)
# print(jihe2)
# 4.集合的查找in/not in
jihe3 = {10, 20, 30, 50}
print(10 in jihe3)
print(10 not in jihe3)
Python集合
最新推荐文章于 2024-05-19 23:31:32 发布