首页 > 解决方案 > 使用字典中的列表

问题描述

我正在尝试在列表中的字典中使用列表。所以我想做的是使用

add_counter = 0
put_lst = []
          while add_counter <= len(add_account_data_mutable)-1:
                put_lst.append(
                    {
                        'PutRequest': {
                            'Item': {
                                list_i_want_to_use[add_counter] #this has a dict type
                            }}
                    })

我一直在以开头的行收到此错误'Item':{

TypeError: unhashable type: 'dict

有人可以帮帮我吗。

标签: pythonlistdictionarynested

解决方案


改变:

'PutRequest': {
    'Item': {
        list_i_want_to_use[add_counter]
    }
}

至:

'PutRequest': {
    'Item': list_i_want_to_use[add_counter]
}

该值list_i_want_to_use[add_counter]已经具有 type dict,因此您可以直接使用它。通过将其包裹在大括号中,您将 a 构造为setdict值作为元素,这是不允许的,因为dict值是不可散列的。


推荐阅读