python OrderedDict 更新具有相同值的键列表

dai*_*yue 2 python dictionary list ordereddictionary python-3.x

OrderedDict我正在尝试更新具有相同值的键列表int,例如

for idx in indexes:
    res_dict[idx] = value
Run Code Online (Sandbox Code Playgroud)

其中value是一个int变量,indexes是slistinta,充当键,res_dict是 an OrderedDict,尝试在一行中解决上述问题,

res_dict[indexes]=value
Run Code Online (Sandbox Code Playgroud)

但得到了错误:

TypeError: unhashable type: 'list'
Run Code Online (Sandbox Code Playgroud)

循环或列表理解是在这里进行此更新的唯一方法吗?

tma*_*aux 7

OrderedDict(以及 dict)提供了update一次更新多个值的方法。

做你想做的事情的最Pythonic方式是:

res_dict.update((idx, value) for idx in indexes)
Run Code Online (Sandbox Code Playgroud)

它将保留您的原始顺序OrderedDict