简体   繁体   English

用另一个字典的值替换字典中的嵌套键

[英]Replace nested keys in a dictionary with values of another dictionary

At the moment I have two dictionaries I want to change the inner keys with the values of the other dictionary 目前,我有两个字典,我想用另一个字典的值更改内部键

d1 = {1:{1: 2.0,2: 1.5,3: 5.0},
      2:{1: 7.5,2: 6.0,3: 1.0}}
d2 = {1: 'a', 2: 'b', 3: 'c'}
expected output: {1:{'a': 2.0,'b': 1.5,'c': 5.0},
                  2:{'a': 7.5,'b': 6.0,'c': 1.0}}

Sadly these two dictionarys are filled with a lot of data and it takes a long time to iterate over d1 and call a method which iterates over d2 to replace the keys in d1. 可悲的是,这两个字典中充满了大量数据,并且要花很长时间才能遍历d1并调用一个遍历d2的方法来替换d1中的键。

Is it possible to change the inner key, value pair in a faster time? 是否可以在更短的时间内更改内键,值对? I found a possibility to replace the keys of a simple dictionary: 我发现可以替换简单词典的键:

d = {'x':1,'y':2,'z':3}
d1 = {'x':'a','y':'b','z':'c'}
d = {d1[k]:v for k,v in d.items()}

output: {'a': 1, 'c': 3, 'b': 2}

but not with a nested dictionary. 但没有嵌套字典。

So now I have no idea how I can solve my problem. 所以现在我不知道如何解决我的问题。 Maybe one of you guys could help me please. 也许你们中的一个可以帮助我。

You may do it using nested dict comprehension as: 您可以使用嵌套的dict理解来做到这一点:

>>> d1 = {1:{1: 2.0,2: 1.5,3: 5.0},
...       2:{1: 7.5,2: 6.0,3: 1.0}}
>>> d2 = {1: 'a', 2: 'b', 3: 'c'}
>>> {a: {d2[k]: v for k, v in d.items()} for a, d in d1.items()}
{1: {'a': 2.0, 'c': 5.0, 'b': 1.5}, 2: {'a': 7.5, 'c': 1.0, 'b': 6.0}}

OR, using simple for loop as: 或者,使用简单的for循环为:

>>> for _, d in d1.items():  # Iterate over the "d1" dict
...     for k, v in d.items():  # Iterate in nested dict
...         d[d2[k]] = v  # Add new key based on value of "d2"
...         del d[k]   # Delete old key in nested dict
... 
>>> d1
{1: {'a': 2.0, 'c': 5.0, 'b': 1.5}, 2: {'a': 7.5, 'c': 1.0, 'b': 6.0}}

Second approach will update the original d1 dict, where as first approach will create the new dict object. 第二种方法将更新原始的d1字典,而第一种方法将创建新的dict对象。

I think the answer is to use dictionary zip which I've come across before. 我认为答案是使用我以前遇到过的字典zip。

Your question can be solved with zip as in this answer. 您可以使用zip来解决您的问题。 Although yours requires an inner level so its slightly different. 尽管您需要一个内部级别,所以它略有不同。

Map two lists into a dictionary in Python 在Python中将两个列表映射到字典中

d1 = {1:{1: 2.0,2: 1.5,3: 5.0},
      2:{1: 7.5,2: 6.0,3: 1.0}}
d2 = {1: 'a', 2: 'b', 3: 'c'}

d1_edited = {} # I prefer to create a new dictionary than edit the existing whilst looping though existing
newKeys = d2.values() # e.g. ['a', 'b', 'c']
for outer in d1.keys(): # e.g. 1 on first iteration
    newValues = d1[outer]  # e.g. e.g. {1: 2.0,2: 1.5,3: 5.0} on first iteration
    newDict = dict(zip(newKeys,newValues)) # create dict as paired up keys and values
    d1_edited[outer] = newDict # assign dictionary to d1_edited using the unchanged outer key.

print d1_edited

output 输出

{1: {'a': 1, 'c': 3, 'b': 2}, 2: {'a': 1, 'c': 3, 'b': 2}}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM