这个问题在这里已经有了答案:
Copying a key/value from one dictionary into another
(2 个回答)
去年关闭。
假设我有一本字典:
mydict = {"color":"green",
"type":"veg",
"fruit":"apple",
"level": 5
}
new_dict = {}
我想将键“颜色”和“水果”及其值附加到
new_dict
中。 .最简单的方法是什么?谢谢你。
最佳答案
最简单的方法是这样的:
new_dict["color"] = mydict["color"]
但是,如果您需要传递给新字典的项目列表更大,则可以使用以下内容:
items = ["color", "fruit"]
for item in items:
new_dict[item] = mydict[item]
关于python - 如何在python中将一个键对值从一个字典复制到另一个字典,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61401885/