【发布时间】:2021-12-12 14:26:36
【问题描述】:
真的很抱歉,当你有项目要做时,在 python 中做一个菜鸟是相当困难的......这是我的问题
d= { 'a': [ '"123728"', '"102359"', '"110663"', '"110664"','"110665"', '"110666"', '"110667"', '"110668"', '"110669"', '"110670"', '"110671"', '"110672"', '"115663"', '"115664"', '"115665"', '"122330"', '"122351"', '"110673"', '"120433"', '"121831"'],
'b': ['"100016"', '"101424"', '"101948"', '"102832"', '"108834"', '"110397"', '"110622"', '"110679"', '"110680"', '"110681"', '"110682"', '"116693"', '"123331"', '"102359"', '"110683"', '"115666"', '"115667"', '"124098"', '"125243"', '"140965"', '"121002"']}
dict d 中的值是 d1 中的键
d1= {'"110674"': '"Apple"', '"110682"': "Banana", '"123331"': "Melon", '"110397"': "Orange"} # correspondance
我想要的是将 d 中的值替换为 d1 中的对应值并删除不对应的值
d3= { 'a': "", 'b': ["Banana", "Melon", "Orange"]}
这是我的代码
for key, value in d.items():
for key2, value2 in d1.items():
for elem in value:
print(key2)
if elem == key2:
full_dict[key] = value2
pprint.pprint(full_dict)
但我有这个结果
defaultdict(None, {'b': 'Orange'})
非常感谢
【问题讨论】:
标签: python list dictionary defaultdict