作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有这两本词典:
Rorys_guests = {“亚当”:2、“布伦达”:3、“大卫”:1、“何塞”:3、“夏洛特”:2、“特里”:1、“罗伯特”:4 }
Taylors_guests = {“大卫”:4、“南希”:1、“罗伯特”:2、“亚当”:1、“萨曼莎”:3、“克里斯”:5}
我想检查 Taylors_guests 中的 key (仅 key )是否已在 Rorys_guests 中。如果没有,我想将键和值对从 Taylors 附加到 Rorys。
注意:两个词典中的某些键是相同的。我不想覆盖 Rorys_guests 中的值。我只想附加 Taylors 字典中尚未包含在 Rorys 字典中的键和值。
for i in Taylors_guests:
print(i)
if i in Rorys_guests:
print("yes")
else:
Rorys_guests = Rorys_guests.get(i)
print(Rorys_guests)
我是一个Python菜鸟,但仍然浏览了许多不同的网站,但找不到解决方案。
提前谢谢您!
最佳答案
Rorys_guests = {**Rorys_guests, **Taylors_guests}
# {'Adam': 2, 'Brenda': 3, 'David': 1, 'Jose': 3, 'Charlotte': 2, 'Terry': 1, 'Robert': 4, 'Nancy': 1, 'Samantha': 3, 'Chris': 5}
关于python - 如何在Python中将一个字典中的新 key 对追加到另一个字典中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60460518/
我是一名优秀的程序员,十分优秀!