python-3.x - 使用二维嵌套字典中的 if/else 语句进行列表理解循环
问题描述
下面的代码片段有效,它产生了我想要的东西,但是我需要一些关于如何使用该行更加 Pythonic 的指针
if avail[employee, day, "Morning"].varValue==0 and
avail[employee, day, "Mid"].varValue==0 and
avail[employee, day, "Night"].varValue==0:
完整代码
Shift_pattern_Master = ["Morning", "Mid", "Night"]
for employee in Employees:
for day in Days:
if avail[employee, day, "Morning"].varValue==0 and
avail[employee, day, "Mid"].varValue==0 and
avail[employee, day, "Night"].varValue==0:
print (f"{employee} on {day} is off.")
else:
for shift in Shift_pattern_Master:
if avail[employee, day, shift].varValue==1:
print (f"{employee} on {day} works in {shift}.")
所以我试图if avail[employee, day, shift].varValue==0 for shift in Shift_pattern_Master:
让它成为一个通用条件,它一直说for
是无效的语法。
我想我错过了什么,但我不知道是什么。感谢您提前提供任何帮助。
解决方案
怎么样:
if all(avail[employee, day, time].varValue==0 for time in ["Morning", "Mid", "Night"]):
另一种选择是重新包装条件:
if (
avail[employee, day, "Morning"].varValue==0
and avail[employee, day, "Mid"].varValue==0
and avail[employee, day, "Night"].varValue==0
):
推荐阅读
- performance - Lucene 内存中空间索引性能不佳
- python - Python:为什么多处理应用线程而不是多处理器?
- java - MockMVC - 如何在没有 mockMvc 发布实际帖子的情况下测试到端点的帖子?
- gradle - 当我引入一个不相关的自定义任务时,为什么这个 gradle buildscript (kotlin dsl) 无法编译?
- javascript - 找出数组元素在javascript中出现的次数并从大到小返回
- swift - 从字典中解析对象数组
- sql-server - 具有可用空间的 SQL Server 备份
- firebase - firebase_admob 0.6.1 的应用 ID 问题
- java - freemarker模板中的日期格式问题
- javascript - 在另一个组件中更改状态