如何在Python中创建一组集合?

编程入门 行业动态 更新时间:2024-10-14 06:22:31
本文介绍了如何在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中创建一组集合?

本文发布于:2023-05-23 15:04:51,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1326421.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:如何在   Python

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!