【问题标题】:How to map/assign/replace values of one dictionary to another dictionary?如何将一个字典的值映射/分配/替换到另一个字典?
【发布时间】: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


    【解决方案1】:

    更新版本:根据 cmets 中提供的信息,我已修改我的解决方案以处理 d2 中相同选择值的多个实例或缺失实例。这是新的解决方案,我相信它会满足要求。我已修改示例数据以使用新功能:

    d1 = {'c_choice1': '1.10', 'c_choice2': '1.20'}
    d2 = {'John': {'strength': 'choice1', 'agility': 'choice1'}}
    d3 = {'John': {'choice1': 'c_choice1', 'choice2': 'c_choice2'}}
    
    d4 = {}
    for k in d3:
        d2x = d2[k]
        d3x = d3[k]
    
        d2y = {v: [] for x in (d2x.values(), d3x.keys()) for v in x}
        for k2 in d2x:
            d2y[d2x[k2]].append(k2)
    
        d4x = {v2 : d1[d3x[k3]] for k3 in d3x for v2 in d2y[k3]}
        d4[k] = d4x
    

    这是d4 的样子:

    {'John': {'agility': '1.10', 'strength': '1.10'}}
    

    注意choice1d2 中出现了两次,而choice2 则不存在。

    【讨论】:

    • 谢谢。但是,如果我的d2d3 是这种形式:d2 = {'John': {'strength': 'choice1', 'agility': 'choice1'}}d3 = {'John': {'choice1': 'c_choice1'}} 并且我想要的输出是{'John': {'agility': '1.10', 'strength': '1.10'}},我应该进行哪些更改?我尝试过使用您的代码,但它不起作用。
    • d3 可以是d3 = {'John': {'choice1': 'c_choice1', 'choice2': 'c_choice2'}} 甚至在d2 中找不到choice2
    • 好的,从原始帖子中并不清楚choice1 可以在a given entry in d2` 中出现多次。我去看看能不能修改一下。
    • 对不起。我已经编辑了我的帖子,并给出了一些例子,表明字典可以有多种不同的形式。
    • 好的,我已经修改了我的解决方案。我相信它现在可以完成您想要的一切。请试一试并告诉我。
    【解决方案2】:

    您可以将其作为单个嵌套的 dict 理解来执行:

    d4 = {name: {stat: d1[d3[name][choice]]
                 for name, choices in d2.items()
                 for stat, choice in stats.items()}
          for name, stats in d2.items()}
    

    但这几乎是不可能理解的。相反,一个循环和一个 dict comp 效果很好:

    d4 = {}
    for name, stats in d2.items():
        d4[name] = {stat: d1[d3[name][choice]] for name, choices in d2.items()
                                               for stat, choice in stats.items()}
    

    【讨论】:

    • 谢谢。但是,如果我的d2d3 是这种形式:d2 = {'John': {'strength': 'choice1', 'agility': 'choice2'}, 'Kevin': {'int': 'choice1'}}d3 = {'John': {'choice1': 'c_choice1', 'choice2': 'c_choice2'}, 'Kevin': {'choice1': 'c_choice1'}},而我想要的输出是:{'John': {'agility': '1.20', 'strength': '1.10'}, 'Kevin': {'int': '1.10'}},我应该做哪些更改?我尝试过使用您的代码,但它不起作用。
    【解决方案3】:

    您需要遍历键并使用 then 来提取数据。例如:

    为了从 d2 中提取名称:

    for name in d2:
        print name
    

    输出
    约翰

    为每个名字提取字典:

    for name  in d2:
        for choice in d2[name]:
            print choice
    

    输出
    敏捷
    实力

    【讨论】:

      猜你喜欢
      • 2017-07-07
      • 1970-01-01
      • 2019-08-27
      • 1970-01-01
      • 2021-03-11
      • 1970-01-01
      • 1970-01-01
      • 2014-08-15
      • 2022-08-17
      相关资源
      最近更新 更多