set()函數(shù)用于創(chuàng)建一個(gè)集合(set),集合是無(wú)序、不重復(fù)的元素的集合。
使用方法如下:
my_set = set()
my_set = set([1, 2, 3, 4, 5])
my_set.add(6)
my_set.remove(1)
set1 = {1, 2, 3}
set2 = {3, 4, 5}
union_set = set1.union(set2) # 求并集
intersection_set = set1.intersection(set2) # 求交集
difference_set = set1.difference(set2) # 求差集