【发布时间】:2010-06-29 15:07:36
【问题描述】:
如何使用另一个字典的键和值数组创建另一个字典?
我想过这样做:
zipped = zip(theExistingDict.keys(), arrayOfValues)
myNewDict = dict(zipped)
但是,这并不完全有效,arrayOfValues 中的每个值都与结果字典中的任意键配对。我无法控制arrayOfValues 中的哪个元素与theExistingDict.keys() 中的哪个键配对。
theExistingDict 看起来像这样:
{u'actual bitrate': 4, u'Suggested Bitrate': 3, u'title': 2, u'id': 1, u'game slot': 0}
arrayOfValues 看起来像这样:
1.0, u'GOLD_Spider Solitaire', u'Spider\\nSolitaire', 120000.0, 120000.0
所以:我希望 arrayOfValues[0] 映射到 game slot(因为在字典中它的值为 0)。
有没有一种简单而优雅的方法来做到这一点?
【问题讨论】:
-
嗯,你想要哪个订单?那么键和值之间有什么联系呢?
-
theExistingDict.keys()会以任意顺序为您提供密钥。您可能必须以某种方式对其进行排序。只有你知道哪些键值对属于一起。 -
@SilentGhost - 请看我上面的编辑。
标签: python dictionary