Python 添加具有相同键但值元组可变列表的两个字典的值
我有两个字典(dict1、dict2),它们有100个相同的键,但元组列表不同,作为值: dict1={'M1':[(50,'M'),(4,'K'),'N2':[(500,'N'),(3,'C'),(7,'K'),'S3':…} dict2={'M1':[(46,'M'),(2,'K'),(11,'F'),'N2':[(400,'N'),(5,'C'),'S3':…} 我想创建一个新字典(dict3),其中,如果值在元组的第一个位置具有相同的字母,则添加值,如果没有,则必须将元组添加到新字典的值中: dict3={'M1':[(96,'M'),(6,'K'),(11,'F'),'N2':[(900,'N'),(8,'C'),(7,'K)],'S3':…} 我在考虑使用python3时会出现类似的情况(此代码不起作用): 我将非常感谢你的帮助。Python 添加具有相同键但值元组可变列表的两个字典的值,python,list,dictionary,tuples,key-value,Python,List,Dictionary,Tuples,Key Value,我有两个字典(dict1、dict2),它们有100个相同的键,但元组列表不同,作为值: dict1={'M1':[(50,'M'),(4,'K'),'N2':[(500,'N'),(3,'C'),(7,'K'),'S3':…} dict2={'M1':[(46,'M'),(2,'K'),(11,'F'),'N2':[(400,'N'),(5,'C'),'S3':…} 我想创建一个新字典(dict3),其中,如果值在元组的第一个位置具有相同的字母,则添加值,如果没有,则必须将元组添加到新字典的值
谢谢。可能有几种方法可以做到这一点。一个相对简单的方法是从元组的内部列表中构造字典——由于键值对是向后的,所以变得稍微复杂 以下是一种方法:
from collections import defaultdict
# Here is your test data
dict1 = {'M1': [(50, 'M'), (4, 'K')], 'N2': [(500, 'N'), (3, 'C'), (7, 'K')]}
dict2 = {'M1': [(46, 'M'), (2, 'K'), (11, 'F')], 'N2': [(400, 'N'), (5, 'C')]
# This dictionary will be you output
dict3 = defaultdict(list)
def generate_inner_dict(lst):
"""This helper takes a list of tuples and returns them as a dict"""
# I used a defaultdict again here because your example doesn't always have
# the same letters in each list of tuples. This will default to zero!
inner_dict = defaultdict(int)
for num, key in lst:
inner_dict[key] = num # Note that the key and value are flipped
return inner_dict
# loop over the keys in one dictionary - i'm assuming the outer keys are the same
for key in dict1:
# use the helper to get two dicts
vals1 = generate_inner_dict(dict1[key])
vals2 = generate_inner_dict(dict2[key])
# loop over the keys...
for inner_key in vals1:
value = vals1[inner_key] + vals2[inner_key] # add them together...
dict3[key].append((inner_key, value)) # and put them in the dictionary
# I turned the defaultdict back into a dict so it looks more obviously like your example
print(dict(dict3))
“此代码不工作”-这不是问题描述。它究竟是如何不起作用的?顺便说一句,
zip(dict1,dict2)
将迭代每个字典的键,这些键是字符串,因此不能用(val1,key1)
解包。如果可以将内部值重新组织为dicts列表,而不是元组列表(以字母为键),则可以大大提高时间复杂度defaultdict(lambda:0)
可以替换为defaultdict(int)
感谢您和@DeepSpace的帮助,该解决方案非常有用。我在dict2中添加了额外的迭代,以检测dict2中键的列表:vals1=generate_inner_dict(dict1[key])vals2=generate_inner_dict(dict2[key]):vals2中键的列表:dict3[key]。append((inner_key,vals2[inner_key])
from collections import defaultdict
# Here is your test data
dict1 = {'M1': [(50, 'M'), (4, 'K')], 'N2': [(500, 'N'), (3, 'C'), (7, 'K')]}
dict2 = {'M1': [(46, 'M'), (2, 'K'), (11, 'F')], 'N2': [(400, 'N'), (5, 'C')]
# This dictionary will be you output
dict3 = defaultdict(list)
def generate_inner_dict(lst):
"""This helper takes a list of tuples and returns them as a dict"""
# I used a defaultdict again here because your example doesn't always have
# the same letters in each list of tuples. This will default to zero!
inner_dict = defaultdict(int)
for num, key in lst:
inner_dict[key] = num # Note that the key and value are flipped
return inner_dict
# loop over the keys in one dictionary - i'm assuming the outer keys are the same
for key in dict1:
# use the helper to get two dicts
vals1 = generate_inner_dict(dict1[key])
vals2 = generate_inner_dict(dict2[key])
# loop over the keys...
for inner_key in vals1:
value = vals1[inner_key] + vals2[inner_key] # add them together...
dict3[key].append((inner_key, value)) # and put them in the dictionary
# I turned the defaultdict back into a dict so it looks more obviously like your example
print(dict(dict3))