【问题标题】:How to solve TypeError: unhashable type 'list'如何解决 TypeError: unhashable type 'list'
【发布时间】:2015-11-18 04:56:06
【问题描述】:

我有两个列表我想修改第一个列表的特定内容并发送输出,但是当我尝试修改它时,出现错误 TypeError: unhashable type: 'list'。 我正在使用 python 和 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"}]

我在这里犯了什么错误?

谁能指导我如何获得所需的输出。

【问题讨论】:

    标签: python mongodb list dictionary hashable


    【解决方案1】:

    您正在使用列表作为字典中的键。 List 不是 python 中的可散列类型。因此,它不能用作字典中的键。只有元组、字符串、数字等可哈希类型才能作为字典中的键。

    【讨论】:

    • 好的,有没有其他方法可以得到想要的结果
    • 你想从 "id" : ["5630baac3f32df134c18b682","564b22373f32df05fc905564"],"phone" : ["9988776655","9638527410"] 中随机选择 id 和 phone 的值吗?跨度>
    • 不仅是第二个列表中的值。我只希望 test 列表中的值出现在输出列表中
    猜你喜欢
    • 2019-07-10
    • 2015-02-11
    • 2012-11-20
    • 2019-10-11
    • 2020-03-27
    • 2020-05-11
    • 1970-01-01
    • 2022-12-05
    相关资源
    最近更新 更多