# 创建一个 frozenset
frozen_set_example = frozenset([1, 2, 3, 4, 5])
# 解释说明:
# frozenset 是 Python 中的一个不可变集合类型。与普通的 set 不同,frozenset 创建后不能被修改,
# 这意味着你不能添加或删除元素。由于它是不可变的,因此可以作为字典的键或另一个集合的元素。
# 打印 frozenset
print(frozen_set_example)
# 尝试修改 frozenset (这将引发 AttributeError)
# frozen_set_example.add(6) # 会抛出 AttributeError: 'frozenset' object has no attribute 'add'
# 可以进行集合操作,例如交集、并集等
set_a = frozenset([1, 2, 3])
set_b = frozenset([3, 4, 5])
# 交集
intersection = set_a.intersection(set_b)
print("交集:", intersection)
# 并集
union = set_a.union(set_b)
print("并集:", union)
# 差集
difference = set_a.difference(set_b)
print("差集:", difference)
上一篇:python cython
下一篇:python 合并列表
Laravel PHP 深圳智简公司。版权所有©2023-2043 LaravelPHP 粤ICP备2021048745号-3
Laravel 中文站