好的,你已经有了一套{c1, c2, c3, ...}
,其中每个cN
本身就是某种集合。
If you don't care about the order of the elements in cN
, but do care that it is unique (disregarding order), then cN
should be a frozenset1 rather than a tuple
:
>>> orig = {("a", "b"), ("b", "a"), ("c", "b"), ("b", "c")}
>>> uniq = {frozenset(c) for c in orig}
>>> uniq
{frozenset(['b', 'a']), frozenset(['b', 'c'])}
一般来说,从 Python 提供的数据类型中选择合适的数据类型比定义和维护自定义类更简单。
1 It can't be a set
, because as a member of a larger set
it needs to be hashable.