【发布时间】:2021-10-26 16:11:00
【问题描述】:
我有两个词典,第一个是展示一些作品:
d= {"a": "pompier", "b": "policier", "c": "tracteur"}
第二个是与列表类型的作品相关的形容词
d1 = {"a": "[gentil, fort]", "b": "[juste, amicale]", "c": "[fonctionnel, fort, utile]"}
我想将 d1 字典中的值附加到 d 字典中,使其看起来像这样
d2 = {"a": "pompier", "[gentil, fort]", "b": "policier", "[juste, amicale]", "c": "tracteur", "[fonctionnel, fort, utile]"}
我需要精确,我不知道(在我的原始文件中)是否存在 d1 中不在 d 字典中的键...
我试过这段代码,但它返回错误
or key, value in d.items():
for key1, value1 in d1.items():
if key in d1:
d1[key].append[value1]
print(d1)
Traceback (most recent call last):
File "<string>", line 7, in <module>
AttributeError: 'str' object has no attribute 'append'
提前谢谢你
【问题讨论】:
-
你的字典 d2 无法定义,你应该重新检查你真正想要的输出
-
你还没有说在 d1 中的键在 d 中不存在的情况下你想要什么行为。举个例子会更清楚。
标签: python loops dictionary