问题描述
我偶然发现了一篇博文,详细介绍了如何在Python中实现powerset函数。所以我尝试着用我自己的方式去做,并发现Python显然不能有一组集合,因为集合不可散列。这很令人厌烦,因为powerset的定义是它是一组集合,我想用实际集合操作来实现它。
I stumbled across a blog post detailing how to implement a powerset function in Python. So I went about trying my own way of doing it, and discovered that Python apparently cannot have a set of sets, since set is not hashable. This is irksome, since the definition of a powerset is that it is a set of sets, and I wanted to implement it using actual set operations.
>>> set([ set() ])
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: unhashable type: 'set'
Python是否有很好的理由可拆分的?
Is there a good reason Python sets are not hashable?
推荐答案
通常,Python中只有不可变对象是可散列的。 set()
- frozenset()
的不可变变体是可散列的。
Generally, only immutable objects are hashable in Python. The immutable variant of set()
-- frozenset()
-- is hashable.
这篇关于为什么Python集不可哈希?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!