这里有很多相关的问题,但没有一个我可以破译,以满足我的标准。所以我要做的是检查一个活动计划是否在计划的\u站点中,如果它是离开它,如果它不是,那么将它添加到计划的\u站点并将仪表板设置为false
样本数据:
>>> scheduled_sites
[{'id': 128, 'scheduled_dashboard': True}, {'id': 61, 'scheduled_dashboard': True}]
>>> active_schedules
[{'id': 1, 'scheduled_dashboard': True},{'id': 61, 'scheduled_dashboard': True}]
预期结果:
[{'id': 128, 'scheduled_dashboard': True}, {'id': 61, 'scheduled_dashboard': True}, {'id': 1, 'scheduled_dashboard': False}]
我觉得我与下面的接近,但得到错误或空白。。。你知道吗
>>> if any(a["id"] == s["id"] for s in scheduled_sites for a in active_schedules):
... s
...
>>> if any(a["id"] == s["id"] for s in scheduled_sites for a in active_schedules):
... a
...
Traceback (most recent call last):
File "<console>", line 2, in <module>
NameError: name 'a' is not defined
>>>
这个怎么样?你知道吗
理解语法只将变量保留在调用
any
的范围内。为了保留变量,您需要显式地定义循环,或者在理解范围内存储结果。你知道吗输出:
a: {'id': 61, 'scheduled_dashboard': True}, a: {'id': 61, 'scheduled_dashboard': True}
s
和a
仅在any(...)
的生成器理解中定义-您不能从外部访问。你知道吗改用普通循环/集合/dict:
输出:
相关问题 更多 >
编程相关推荐