lst = [1, 2, 2, 3, 4, 4, 5]
unique_set = set(lst)
print(unique_set)
输出结果:
{1, 2, 3, 4, 5}
set1 = {1, 2, 3, 4, 5}
set2 = {4, 5, 6, 7, 8}
# 交集
intersection = set1 & set2
print(intersection)
# 并集
union = set1 | set2
print(union)
# 差集
difference = set1 - set2
print(difference)
输出结果:
{4, 5}
{1, 2, 3, 4, 5, 6, 7, 8}
{1, 2, 3}
s = set([1, 2, 3, 4, 5])
print(s)
输出结果:
{1, 2, 3, 4, 5}
set1 = {1, 2, 3}
set2 = {3, 2, 1}
if set1 == set2:
print("The sets are equal")
else:
print("The sets are not equal")
输出结果:
The sets are equal
set1 = {1, 2, 3}
set2 = {1, 2, 3, 4, 5}
if set1.issubset(set2):
print("set1 is a subset of set2")
if set2.issuperset(set1):
print("set2 is a superset of set1")
输出结果:
set1 is a subset of set2
set2 is a superset of set1