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 = {3, 4, 5, 6, 7}
union_set = set1.union(set2)
intersection_set = set1.intersection(set2)
difference_set = set1.difference(set2)
print(union_set) # 輸出:{1, 2, 3, 4, 5, 6, 7}
print(intersection_set) # 輸出:{3, 4, 5}
print(difference_set) # 輸出:{1, 2}
s = {1, 2, 3, 4, 5}
if 3 in s:
print("3存在于集合中")
lst = [1, 2, 2, 3, 4, 4, 5]
if len(lst) == len(set(lst)):
print("列表中沒有重復元素")
else:
print("列表中存在重復元素")
lst = [1, 2, 2, 3, 4, 4, 5]
unique_elements_count = len({x for x in lst})
print(unique_elements_count) # 輸出:5