Python常用数据结构-集合——2.4 集合方法 discard()


集合方法 discard()
discard(item):从集合中移除指定对象 item。
入参:指定对象值
返回:None
元素 item 不存在没影响,不会抛出 KeyError 错误。

# # 1、删除已存在的元素
st = {1, 2, 3, 4, 5}
st.remove(2)
print(st)
# # 2、删除不存在的元素,没有报错
st.discard(1024)
print(st)

返回