[英]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.