- mongodb - 在 MongoDB mapreduce 中,如何展平值对象?
- javascript - 对象传播与 Object.assign
- html - 输入类型 ="submit"Vs 按钮标签它们可以互换吗?
- sql - 使用 MongoDB 而不是 MS SQL Server 的优缺点
我在从另一个字典开始填充 python 字典时遇到了麻烦。
假设“源”字典有字符串作为键,每个值都有一个自定义对象列表。
我正在创建我的目标字典,就像我创建“源”字典一样,这怎么可能不起作用?
我明白了
TypeError: unhashable type: 'list'
代码:
aTargetDictionary = {}
for aKey in aSourceDictionary:
aTargetDictionary[aKey] = []
aTargetDictionary[aKey].extend(aSourceDictionary[aKey])
错误在这一行:aTargetDictionary[aKey] = []
最佳答案
您给出的错误是由于在python中,dictionary keys
必须是immutable types
(如果key可以改变,就会出现问题),并且列出是一个可变类型。
您的错误表明您尝试使用列表作为字典键,<strong>如果您想将列表作为键放入字典中,则必须将列表更改为元组。
根据the python doc :
The only types of values not acceptable as keys are values containinglists or dictionaries or other mutable types that are compared byvalue rather than by object identity, the reason being that theefficient implementation of dictionaries requires a key’s hash valueto remain constant
关于Python 字典 : TypeError: unhashable type: 'list' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8532146/
我是 python 的新手。我可以取消哈希,或者更确切地说,我怎样才能取消哈希值。我正在使用 std hash() 函数。我想做的是首先散列一个值,将它发送到某个地方,然后再散列它: #process
不完全确定它是否与 scipy 相关,这就是我包含 python 标签的原因。 我有一个数据网格。每当我尝试访问网格中的某些内容时,如下所示: d = pd.read_csv('Debugdata1.
abc = {} abc[int: anotherint] 然后错误就来了。类型错误:不可散列的类型?为什么我收到这个?我试过 str() 最佳答案 这似乎是一个语法问题: >>> abc = {}
我正在尝试获取元组列表:类似于 [ [(1,0),(2,0),(3,0)],[(1,1),(2 ,1),(3,1)....]]我用了这个说法 set([(a,b)for a in range(3)]f
我试图分割一个字符串,然后从该字符串中取出一个关键字并在字典中找到它。然后我想调出字典的那部分,但我遇到了这个错误 - TypeError: unhashable type:set -最后一行: so
我偶然发现了this question并想知道为什么我不能这样做: # this does not work pipeline = { {"resample" : {"type" : "tra
我正在尝试使用以下数据框运行回归 dfMyRoll 数据框的头部看起来像: SCORE SCORE_LAG date
我正在使用用 Python 编写的数学包 SAGE。 我正在尝试操作一些向量集,但我不断收到上述错误消息。我完全不知道它在告诉我什么,尽管我确实理解可变的含义有人可以用相当简单的概念术语解释它告诉我的
我正在尝试运行一个有效执行以下操作的程序: if [4, 5, False, False, False, False] in {} 而且,在这一行中,我得到了一个 TypeError: unhasha
board = [["bRook", "bKnight", "bBishop", "bQueen", "bKing", "bBishop", "bKnight", "bRook"],
我有一个数据框,还包含空间信息(坐标)。由于某种原因,尽管 gemoetry 列具有相同的信息,但它并不被视为相同,因此会出现错误。 FID_buffer geometry 0 140 LINE
我正在尝试从文件中收集错误,并且需要对它们进行计数这是我需要的输出:[ error1 : 6 , error2 : 3 , error3 : 2] import os,sys import colle
我在我的程序中收到以下错误:追溯: Traceback (most recent call last): File "C:\Python33\Archive\PythonGrafos\Alpha.py
这段代码给我一个错误unhashable type: dict 谁能给我解释一下解决方案是什么? negids = movie_reviews.fileids('neg') def word_feat
我有一个非常简单的字典,上面有一些数据: some_data= {'totalsForAllResults': {'ga:sessions': '11'}, 'profileInfo': {'inte
我正在尝试用 Python 实现缓存功能。代码如下所示: def memoize(func): """Store the results of the decorated function f
所以我有一个散列函数的代码,从它的外观来看,没有办法简单地取消散列它(很多按位 AND、OR、Shift 等)。我的问题是,如果我需要在被散列之前找出原始值,是否有比暴力破解一组可能值更有效的方法?
更新:我正在使用“Socrata Open Source API”中的一些示例代码。我在代码中注意到以下注释: # First 2000 results, returned as JSON from
尽管我从未在代码中的任何地方使用过字典,但我收到了有关字典的错误消息 这是我的代码: mm=[] soln=[] for i in range(len(momvec)): string = s
我下载了 Kaggle Kernel作为 Jupyter Notebook 文件,我尝试在本地系统上运行。内核在 Kaggle 上运行良好。但是,当我尝试将其作为 .ipynb 文件运行时,以下行(在
我是一名优秀的程序员,十分优秀!