集合(Set)
创建集合
# 创建包含元素的集合
my_set = {1, 2, 3, 'hello', (4, 5)}
print(my_set) # 输出顺序可能与输入不同,例如 {1, 2, 3, (4, 5), 'hello'}
# 创建空集合
empty_set = set()
print(type(empty_set)) # <class 'set'>
print(empty_set) # set()# 从字符串创建 (字符去重)
char_set = set("hello")
print(char_set) # {'l', 'o', 'e', 'h'} 或类似顺序
# 从列表创建 (元素去重)
list_set = set([1, 2, 2, 3, 3, 3])
print(list_set) # {1, 2, 3}
# 从元组创建
tuple_set = set((1, 2, 3))
print(tuple_set) # {1, 2, 3}
# 创建空集合
empty_set = set()基本操作
集合运算
集合推导式
不可变集合
最后更新于