我有一个输入字典:
dict1 = {'AM': ['tv', 'rs', 'pq', 'MN', 'tN', 'tq', 'OP', 'tP', 'QR',
'tr','fs','nz','tz','dz'],
'BR': ['tv', 'rs', 'pq', 'MN', 'tN', 'tq', 'OP', 'tP', 'QR',
'tr''fs','nz','tz','dz'],
'ZR':'[tv', 'rs', 'pq', 'MN', 'tN', 'tq', 'OP', 'tP', 'QR',
'tr','fs','nz','tz','dz']}
需要查找的另外两个字典:
dict2 = {'AM':{'pq':1.2,'rs':2.41,'tv':3.81},'BR':{'MN':1.3,'OP':1.41,'QR':1.81},'ZR':
{'fs':1.2,'nz':1.5,'tz':1.7,'dz':1.3}}
dict3 = {'AM':{'tq':1.3,'rs':1.41,'tv':2.41},'BR':{'tN':1.8,'tP':1.81,'tr':1.42}}
所需输出
{'AM-tv': (3.81,2.41),
'AM-rs': (2.41,1.41),
'AM-pq': (1.2,'sert'),
'AM-MN': ('sert','sert'),
'AM-tN': ('sert','sert'),
'AM-tq': ('sert',1.3),
'AM-OP': ('sert','sert'),
'AM-tP': ('sert','sert'),
'AM-QR':- ('sert','sert'),
'AM-tR':- ('sert','sert'),
'AM-fs':- ('sert','sert'),
'AM-nz':- ('sert','sert'),
'AM-tz':- ('sert','sert'),
'AM-dz': ('sert','sert'),
'ZR-tv': ('sert','sert'),
'ZR-rs': ('sert','sert'),
'ZR-pq': ('sert','sert'),
'ZR-MN': ('sert','sert'),
'ZR-tN': ('sert','sert'),
'ZR-tq': ('sert','sert'),
'ZR-OP': ('sert','sert'),
'ZR-tP': ('sert','sert'),
'ZR-QR': ('sert','sert'),
'ZR-tr': ('sert','sert'),
'ZR-fs': (1.2,'sert'),
'ZR-nz': (1.5,'sert'),
'ZR-tz':(1.7,'sert'),
'ZR-dz': (1.3,'sert')
'BR-tv': ('sert','sert'),
'BR-rs': ('sert','sert'),
'BR-pq': ('sert','sert'),
'BR-MN': (1.3,'sert'),
'BR-tN': ('sert',1.8),
'BR-tq': ('sert','sert'),
'BR-OP': (1.41,'sert'),
'BR-tP': ('sert',1.81),
'BR-QR': (1.81,'sert'),
'BR-tr': ('sert',1.42),
'BR-fs':- ('sert','sert'),
'BR-nz':- ('sert','sert'),
'BR-tz':- ('sert','sert'),
'BR-dz': ('sert','sert')}
在输出中,需要生成dict1对。如果dic1的值存在于dic2或dic3的内部嵌套字典键中,则它将替换为内部嵌套字典值,否则它将替换为'sert'字符串。dic2和dic3中的外部字典键不相同。有什么办法吗?我试过的代码是这样的:
out = {}
for k, lst in dict1.items():
for v in lst:
out[f"{k}-{v}"] = (dict2[k].get(v, 'sert'),
dict3[k].get(v, 'sert'))
但是这不起作用,但是我的外部字典键是不同的。
将包含浮点数的两个字典平放为值。然后,使用列表推导式生成结果中的所有键。最后,构建结果:
flat_dict2 = {f"{k}-{inner_k}": v for k in dict2 for inner_k, v in dict2[k].items()}
flat_dict3 = {f"{k}-{inner_k}": v for k in dict3 for inner_k, v in dict3[k].items()}
result_keys = [f"{k}-{inner_k}" for k in dict1 for inner_k in dict1[k]]
{key: (flat_dict2.get(key, "sert"), flat_dict3.get(key, "sert")) for key in result_keys}
输出(只写出前三个和最后三个键值对,因为输出很长):
{
'AM-tv': (3.81, 2.41), 'AM-rs': (2.41, 1.41), 'AM-pq': (1.2, 'sert'),
...
'ZR-nz': (1.5, 'sert'), 'ZR-tz': (1.7, 'sert'), 'ZR-dz': (1.3, 'sert')
}
您可以这样做(尽管结果的顺序与期望的不同,但这在字典中并不重要):
result = {}
for i, j in dict1.items():
for k in j:
result[f'{i}-{k}'] = []
for m in dict2, dict3:
try:
result[f'{i}-{k}'].append(m[i][k])
except KeyError:
result[f'{i}-{k}'].append('sert')
尝试不进行如下预处理,您需要两个dict.get()
和dict1
的搜索基键和dict1
的value_list
的值
res = {}
for k1,v1 in dict1.items():
for val in v1:
res[f'{k1}_{val}'] = (
dict2.get(k1,{}).get(val, 'sert'),
dict3.get(k1,{}).get(val, 'sert')
)
print(res)
或者一行:
res = {f'{k1}_{val}' : (dict2.get(k1,{}).get(val, 'sert'), dict3.get(k1,{}).get(val, 'sert')) for k1,v1 in dict1.items() for val in v1}
输出:
{'AM_tv': (3.81, 2.41), 'AM_rs': (2.41, 1.41), 'AM_pq': (1.2, 'sert'),
'AM_MN': ('sert', 'sert'), 'AM_tN': ('sert', 'sert'), 'AM_tq': ('sert', 1.3),
'AM_OP': ('sert', 'sert'), 'AM_tP': ('sert', 'sert'), 'AM_QR': ('sert', 'sert'),
'AM_tr': ('sert', 'sert'), 'AM_fs': ('sert', 'sert'), 'AM_nz': ('sert', 'sert'),
'AM_tz': ('sert', 'sert'), 'AM_dz': ('sert', 'sert'), 'BR_tv': ('sert', 'sert'),
'BR_rs': ('sert', 'sert'), 'BR_pq': ('sert', 'sert'), 'BR_MN': (1.3, 'sert'),
'BR_tN': ('sert', 1.8), 'BR_tq': ('sert', 'sert'), 'BR_OP': (1.41, 'sert'),
'BR_tP': ('sert', 1.81), 'BR_QR': (1.81, 'sert'), 'BR_trfs': ('sert', 'sert'),
'BR_nz': ('sert', 'sert'), 'BR_tz': ('sert', 'sert'), 'BR_dz': ('sert', 'sert'),
'ZR_tv': ('sert', 'sert'), 'ZR_rs': ('sert', 'sert'), 'ZR_pq': ('sert', 'sert'),
'ZR_MN': ('sert', 'sert'), 'ZR_tN': ('sert', 'sert'), 'ZR_tq': ('sert', 'sert'),
'ZR_OP': ('sert', 'sert'), 'ZR_tP': ('sert', 'sert'), 'ZR_QR': ('sert', 'sert'),
'ZR_tr': ('sert', 'sert'), 'ZR_fs': (1.2, 'sert'), 'ZR_nz': (1.5, 'sert'),
'ZR_tz': (1.7, 'sert'), 'ZR_dz': (1.3, 'sert')}