本文介绍了如何解决TypeError:无法散列的类型“列表"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个列表,我想修改第一个列表的特定内容并发送输出,但是当我尝试修改它时,出现错误TypeError: unhashable type: 'list'.我在mongodb中使用python.

I have two lists I would like to modify a particular content of the first list and send the output but when I try to modify it, am getting an errorTypeError: unhashable type: 'list'.I am using python with mongodb.

这两个列表是

data = [{"id" : ["5630baac3f32df134c18b682","564b22373f32df05fc905564"],"phone" : ["9988776655","9638527410"], "Request": "Support staff", "Date": "19-11-2015"}]
test = [{"phone" : "9638527410", "id": "5630baac3f32df134c18b682"}]
id = {}
for info in chain(data, test):
    id.setdefault(info['id'], {}).update(info)
res = list(id.values())
print(res)

所需的输出是

[{"phone" : "9638527410", "id": "5630baac3f32df134c18b682", "Request": "Support staff", "Date": "19-11-2015"}]

我在这里犯了什么错误?

What is the mistake I am making here?

任何人都可以指导我如何获得所需的输出.

Can anyone guide me how I can get the desired output.

推荐答案

您正在将list用作字典中的键.列表不是python中的可哈希类型.因此,它不能用作字典中的键.只能将元组,字符串,数字之类的可哈希类型用作字典中的键.

You are using list as an key in the dictionary. List is not a hashable type in python. So, it can not be used as key in the dictionary. Only hashable types such as tuple, strings, numbers can be used as key in the dictionary.

这篇关于如何解决TypeError:无法散列的类型“列表"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-19 18:23