问题描述
我正在尝试重组一个字典,并将值与其键之后的变量名称相关联。像
I am trying to 'destructure' a dictionary and associate values with variables names after its keys. Something like
params = {'a':1,'b':2}
a,b = params.values()
但由于没有订购字典,所以不能保证params.values()以(a,b)的顺序返回值。有没有一个很好的方式呢?
but since dictionaries are not ordered, there is no guarantee that params.values() will return values in the order of (a,b). Is there a nice way to do this?
谢谢
推荐答案
如果您担心使用本地字典时遇到的问题,并且您更喜欢遵循原始策略,则从python 2.7和3.1 允许您按照首次插入的顺序恢复字典项目
If you are afraid of the issues involved in the use of the locals dictionary and you prefer to follow your original strategy, Ordered Dictionaries from python 2.7 and 3.1 collections.OrderedDicts allows you to recover you dictionary items in the order in which they were first inserted
这篇关于python destructuring-bind词典内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!