【发布时间】:2017-09-30 06:37:32
【问题描述】:
我有一个字典作为全局变量和一个字符串列表:
GLOBAL = {"first": "Won't change", "second": ""}
words = ["a", "test"]
我的目标是创建以下列表:
[{"first": "Won't change", "second": "a"}, {"first": "Won't change", "second": "test"}]
我可以使用以下代码:
result_list = []
for word in words:
dictionary_to_add = GLOBAL.copy()
dictionary_to_add["second"] = word
result_list.append(dictionary_to_add)
我的问题是如何使用理解列表或使用 map() 函数来做到这一点
【问题讨论】:
-
你的代码不会给你想要的结果。
-
@DanielRoseman 修复了它
标签: python python-3.x dictionary list-comprehension map-function