set and frozen set

Set

set 的初始化要注意, 要带上 [], 不然结果不一样!

>>> s = set([(1,2)])
>>> s
{(1, 2)}
>>> s.add((2,3))
>>> s
{(1, 2), (2, 3)}

# 上面才是我们要的, set of tuples

>>> w = set((1,2))
>>> w
{1, 2}
>>> w.add((2,3))
>>> w
{1, 2, (2, 3)}

# 这个 (1, 2) 并没有存成 tuple

Frozen Set

Frozen Sets are immutable objects that only support methods and operators that produce a result without affecting the frozen set or sets to which they are applied.

# A frozen set 
frozen_set = frozenset(["e", "f", "g"])

Last updated