【发布时间】:2016-06-27 23:42:26
【问题描述】:
假设我有以下 3 个字典:
General case:
d1 = {'c_choice1': '1.10', 'c_choice2': '1.20',...}
d2 = {'John': {'strength': 'choice1', 'agility': 'choice2',...},...}
d3 = {'John': {'choice1': 'c_choice1', 'choice2': 'c_choice2',...},...}
d2 and d3 can be:
d2 = {'John': {'strength': 'choice1', 'agility': 'choice2'}, 'Kevin': {'int': 'choice1'}}
d3 = {'John': {'choice1': 'c_choice1', 'choice2': 'c_choice2'}, 'Kevin': {'choice1': 'c_choice1'}}
or
d2 = {'John': {'strength': 'choice1', 'agility': 'choice1'}}
d3 = {'John': {'choice1': 'c_choice1'}}
我想做什么(以一般情况为例):
a) 从 d3,让choice1 = c_choice1,choice2 = c_choice2,等等。
choice1 = c_choice1
choice2 = c_choice2
b) 查找 d1 并找到 c_choice1、c_choice2 等的值。
c_choice1 = 1.10
c_choice2 = 1.20
c) 创建一个最终字典,将 d2 中的choice1、choice2 等替换为 d1 中的值
我想要的输出(以一般情况为例):
d4 = {'John': {'strength': '1.10', 'agility': '1.20',...}...}
注意:
a) 3个字典是从输入文件中提取数据时创建的,键和值不是我自己定义的,因此我不知道字典中键和值的顺序。
b) 我使用的是 Python 2.7。
我该怎么做?
【问题讨论】:
标签: python python-2.7 dictionary