【发布时间】:2020-08-11 16:03:10
【问题描述】:
我需要添加到嵌套字典的开头。看起来 move_to_end() 是我们完成此任务的最简单方法,但似乎我不能在嵌套字典中使用它。
dict = OrderedDict({
'abdomen' : {"liver":3 , "spleen":1},
})
dict['abdomen'].update({'stomach':'2'})
dict['abdomen'].move_to_end('stomach', last = False)
print(dict['abdomen'])
产生错误: 回溯(最近一次通话最后): 文件“test.py”,第 232 行,在 dict['abdomen'].move_to_end('stomach', last = False) AttributeError: 'dict' 对象没有属性 'move_to_end'
【问题讨论】:
标签: python-3.x dictionary nested ordereddictionary