我有两个字典列表,一个是项目标识符列表,另一个是已完成项目标识符的列表。我希望根据已完成列表中的存在向项目标识符列表中添加一个键。
当前代码
>>> projects = [{'id': 1}, {'id': 2}, {'id': 3}]
>>> completes = [{'id': 1}, {'id': 2}]
>>> for complete in completes:
... for project in projects:
... if project["id"] == complete["id"]:
... project["complete"] = 1
... else:
... project["complete"] = 0
...
>>> print projects
[{'id': 1, 'complete': 0}, {'id': 2, 'complete': 1}, {'id': 3, 'complete': 0}]
预期产出
[{'id': 1, 'complete': 1}, {'id': 2, 'complete': 1}, {'id': 3, 'complete': 0}]
项目被标记为完成后,如何跳出嵌套循环?我应该考虑另一种方法而不是使用嵌套循环吗?