简体   繁体   English

如何根据另一个字典中指定给相同键的值来更改字典键?

[英]How to Change a Dictionaries Key Based on the Value Designated to the Same Key in Another Dictionary?

I am trying to update a dictionary key using key value pairs in another dictionary. 我正在尝试使用另一个字典中的键值对更新字典键。 The two dictionaries I am trying to combine are both nested into lists: 我要合并的两个字典都嵌套在列表中:

dictionary1 = [{ '32639': {'78549': {'526' : { 'sum': 8930.40, 'min' : 2380, 'max': 74839}}}} , {'89304': {'20384': {'152' : { 'sum': 51235.20, 'min' : 4512, 'max': 362.69}}}}, { '41526': {'45315': {'364' : { 'sum': 8985.65, 'min' : 3632.32, 'max': 4558.15}}}}]

dictionary2 = [{'32639':'90283'}, {'49034': '89203'}, {'28942': '39024'}, {'41526':'24903'} ]

I want the resulting dictionary to look exactly like dictionary1 however if the key of the dictionaries in dictionary1 is in the keys in the dictionaries of dictionary2 they should be changed. 我希望生成的字典看起来完全像dictionary1,但是如果dictionary1中字典的关键字在dictionary2中字典的关键字中,则应该对其进行更改。

Resulting dictionary: 结果字典:

new_dictionary = [{ '90283': {'78549': {'526' : { 'sum': 8930.40, 'min' : 2380, 'max': 74839}}}} , {'89304': {'20384': {'152' : { 'sum': 51235.20, 'min' : 4512, 'max': 362.69}}}}, { '24903': {'45315': {'364' : { 'sum': 8985.65, 'min' : 3632.32, 'max': 4558.15}}}}]

I have attempted: 我尝试过:

list1 = []
for d1, d2 in zip(dictionary1, dictionary2):
    for key, value in d1.iteritems():
        new_dict = {}
        if key in d2:
            new_dict[d2[key]] = value
            list1.append(new_dict)
        else:
            new_dict[key] = value
            list1.append(new_dict)

However it is not working, on this sample data it works however, the program only iterates through dictionary1 based on the length of dictionary2. 但是,它不起作用,但是在此示例数据上却起作用,但是该程序仅基于dictionary2的长度来循环访问dictionary1。 So I am trying to run this with a list that has 841 dictionaries (dictionary 1) and a list of dictionaries of 53 (dictionary 2) and it will only convert the first 53 keys in dictionary 1 before it quits. 因此,我尝试使用包含841个词典(词典1)的列表和53个词典(词典2)的列表来运行此列表,并且它只会在退出之前转换词典1中的前53个键。

Try something like this: 尝试这样的事情:

ds1 = [{ '32639': {'78549': {'526' : { 'sum': 8930.40, 'min' : 2380, 'max': 74839}}}} , {'89304': {'20384': {'152' : { 'sum': 51235.20, 'min' : 4512, 'max': 362.69}}}}, { '41526': {'45315': {'364' : { 'sum': 8985.65, 'min' : 3632.32, 'max': 4558.15}}}}]

ds2 = [{'32639':'90283'}, {'49034': '89203'}, {'28942': '39024'}, {'41526':'24903'} ]

def trans(ds1, ds2):
    for d1 in ds1:
        for d2 in ds2:
            ckeys = d1.keys() & d2.keys()
            ukeys = d1.keys() - d2.keys()
            for ck in ckeys:
                yield (d2[ck], d1[ck])
            for uk in ukeys:
                yield (uk, d1[uk])
print(dict(trans(ds1, ds2)))

I got output: 我得到了输出:

{'90283': {'78549': {'526': {'sum': 8930.4, 'min': 2380, 'max': 74839}}}, '32639': {'78549': {'526': {'sum': 8930.4, 'min': 2380, 'max': 74839}}}, '89304': {'20384': {'152': {'sum': 51235.2, 'min': 4512, 'max': 362.69}}}, '41526': {'45315': {'364': {'sum': 8985.65, 'min': 3632.32, 'max': 4558.15}}}, '24903': {'45315': {'364': {'sum': 8985.65, 'min': 3632.32, 'max': 4558.15}}}}

暂无
暂无

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

相关问题 如何根据另一个字典替换列表字典中键的值? - How to replace a value for a key in dictionaries of a list based on another dictionary? 有没有办法根据一个字典中的值小于另一个字典中的相同键来过滤字典列表? - Is there a way to filter a list of dictionaries based on a value in one dictionary being less than the same key in another? 根据另一个字典列表中的键值删除字典 - Delete a dictionary based on the value of a key in another list of dictionaries 根据同一个字典中的另一个,提取熊猫中一个字典键的值 - Extracting value for one dictionary key in Pandas based on another in the same dictionary 如果字典的键值有另一个相同的键值,你如何组合字典的键值,同时省略未组合的字典? - How do you combine dictionaries' key values if they have another key value that is the same while leaving out the dictionary that was not combined? 如何根据另一个键的值在字典中选择一个键的值 - How to select a value of a key in dictionary based on the value of another key 根据基于该字典中另一个键的值的条件,更新python词典列表中的值 - Update a value in a list of dictionaries in python based on a condition based on the value of another key in that dictionary 将键更改为其他字典中相同键的值 - Change key to value of same key in other dictionary 如何将字典列表附加到字典中的键值? - How to append a list of dictionaries to the value of a key in a dictionary? 嵌套字典:将字典中相同键的值的总和嵌套为字典值 - Nested dictionary: sum values of same key of the dictionaries nested as a value of dictionary
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM