本文介绍了如何在Python中创建一组集合?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想在Python中创建一组集合。
I'm trying to make a set of sets in Python. I can't figure out how to do it.
从空集开始 xx :
xx = set([]) # Now we have some other set, for example elements = set([2,3,4]) xx.add(elements)/ p>
but I get
TypeError: unhashable type: 'list'或
TypeError: unhashable type: 'set'在Python中有可能有一组集合吗?
Is it possible to have a set of sets in Python?
我处理大集合的集合,并且我想能够不必处理重复集合(集合A1,A2,...,A n的集合B将如果A i = A i将取消两个集合) p>
I am dealing with a large collection of sets and I want to be able to not have to deal duplicate sets (a set B of sets A1, A2, ...., An would "cancel" two sets if Ai = Aj)
推荐答案Python的抱怨,因为内部设置对象是可变的,解决方案是使用 frozenset 作为内部集合,表示您无意修改它们。
Python's complaining because the inner set objects are mutable and thus not hashable. The solution is to use frozenset for the inner sets, to indicate that you have no intention of modifying them.
更多推荐
如何在Python中创建一组集合?
发布评论