# 集合无序不重复
my_set = {"qq","cc","3",5,7}
#新增
my_set.add("ttt")
print(my_set)
#移除元素
my_set.remove(5)
print(my_set)
#随机取出一个元素
print(my_set.pop())
# 取两个集合差集会得到一个新集合 A集合有但B集合没有的
my_set_2 = {"3",5,7}
print(my_set.difference(my_set_2))
# 消除两个集合差集,A消除B不变
my_set = {"qq","cc","3",5,7}
my_set_2 = {"3",5,7}
print(my_set.difference_update(my_set_2))
print(f"消除后的myset{my_set}")
print(f"消除后的myset2{my_set_2}")
# 合并两个集合成一个新集合
print(my_set.union(my_set_2))
#集合遍历
for i in my_set:
print(i)
运行结果:
{'qq', '3', 'cc', 5, 7, 'ttt'}
{'qq', '3', 'cc', 7, 'ttt'}
qq
{'ttt', 'cc'}
None
消除后的myset{'qq', 'cc'}
消除后的myset2{'3', 5, 7}
{'qq', '3', 'cc', 5, 7}
qq
cc