python - 列表/集合转换出错,为什么?
问题描述
这段代码:
testset = list(set(child).union(pot_deck))
曾经工作,制作testset
一个由两个列表child
和pot_deck
. 我在代码中更改了一些看似无关的内容,现在它带有一个
TypeError: unhashable type: 'list'
.
我试过了
testset = list(set(child).union(set(pot_deck)))
也是,但结果相同。
解决方案
具有简单原始值的列表应该是可散列的,至少它们在 Python 3.8 中:
In [5]: child = [1]
In [6]: pot_deck = [2]
In [7]: testset = list(set(child).union(pot_deck))
...:
In [8]: testset
Out[8]: [1, 2]
但是,如果您有一个列表列表,您将获得:
In [9]: child = [[1]]
In [10]: testset = list(set(child).union(pot_deck))
...:
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-10-52828817787d> in <module>
----> 1 testset = list(set(child).union(pot_deck))
TypeError: unhashable type: 'list'
所以我认为这就是发生的事情。child
你可以把里面的内容打印出来post_deck
找出来。
推荐阅读
- java - 如何获取jar文件中文件的文件路径?
- sql - 在 Oracle SQL 中,用第二个表中的值替换可变数量的占位符
- javascript - Three.js中改变displacementMap的displacementScale时如何添加过渡
- python - 如何使用 ffmpeg-python 创建具有视频和音频延迟的并排水平堆叠视频?
- swiftui - Facebook Login SwiftUI 未登录并循环返回上一页
- api - 在查询参数中接收一个锯齿状数组
- python - 在 Python 中对多级 orderdict 进行排序?
- javascript - VueJS - 如何处理多组复选框,使用 v-for 呈现
- groovy - JIRA ScriptRunner 的自定义脚本字段
- jupyter-notebook - 在 Jupyter Book 中使用 pdflatex 构建器时,如何获得要显示的图表?