python frozenset 是一个不同的hashable objects的无序集合。 frozenset是一个不可变的集体, 因此在创建后不能修改其内容。
class set([iterable])class frozenset([iterable])Return a new set or frozenset object whose elements are taken from iterable. The elements of a set must be hashable. To represent sets of sets, the inner sets must be frozenset objects. If iterable is not specified, a new empty set is returned.Instances of set and frozenset provide the following operations:len(s)Return the number of elements in set s (cardinality of s).x in sTest x for membership in s.x not in sTest x for non-membership in s.isdisjoint(other)Return True if the set has no elements in common with other. Sets are disjoint if and only if their intersection is the empty set.issubset(other)set <= otherTest whether every element in the set is in other.set < otherTest whether the set is a proper subset of other, that is, set <= other and set != other.issuperset(other)set >= otherTest whether every element in other is in the set.set > otherTest whether the set is a proper superset of other, that is, set >=other and set != other.union(*others)set | other | ...Return a new set with elements from the set and all others.intersection(*others)set & other & ...Return a new set with elements common to the set and all others.difference(*others)set - other - ...Return a new set with elements in the set that are not in the others.symmetric_difference(other)set ^ otherReturn a new set with elements in either the set or other but not both.copy()Return a new set with a shallow copy of s.
In [4]: vowels = ['1', 'a', 'b','.']In [5]: fs = frozenset(vowels)In [6]: fsOut[6]: frozenset({'.', '1', 'a', 'b'})In [7]:In [7]: vowels_tuple = (1, 2, 3, 4, 5, 4, 3)In [8]: fs2 = frozenset(vowels_tuple)In [9]: fs2Out[9]: frozenset({1, 2, 3, 4, 5})
因为frozenset是不可变的,所以没有方法可以改变它的元素。因此,add()、remove()、update()、pop()等函数没有为frozenset定义。
