集合初始化

列表和集合 / 集合(Set)

int_set = {1, 2, 3}
str_set = {"one", "two", "three"}
# 没有顺序:用 sorted() 让输出保持稳定
str_sorted = sorted(str_set)
# str_sorted is ['one', 'three', 'two']

# 空集合:{} 会是 dict
empty_set = set()
# empty_set is set()
not_set = {}
# type(not_set) is <class 'dict'>

# 任何可迭代对象,重复项会被丢弃
from_list = set([1, 2, 2, 3, 3, 3])
# from_list is {1, 2, 3}

print(f'{int_set = }')
print(f'{str_sorted = }')
print(f'{empty_set = }')
print(f'{type(not_set) = }')
print(f'{from_list = }')