在Python中,set是一個無序且唯一的集合數據類型。可以使用以下方法來創建和操作set:
my_set = set()
my_set = {1, 2, 3}
my_list = [1, 2, 3]
my_set = set(my_list)
my_set.add(4)
my_set.remove(3)
if 2 in my_set:
print("2 is in the set")
set1 = {1, 2, 3}
set2 = {2, 3, 4}
intersection = set1.intersection(set2)
union = set1.union(set2)
difference = set1.difference(set2)
print(intersection)
print(union)
print(difference)
my_set.clear()
for item in my_set:
print(item)
以上是一些使用set的基本方法,可根據具體需求進一步學習和使用。