Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 3.x 使用二维嵌套字典中的if/else语句列出理解循环_Python 3.x_For Loop_If Statement_List Comprehension - Fatal编程技术网

Python 3.x 使用二维嵌套字典中的if/else语句列出理解循环

Python 3.x 使用二维嵌套字典中的if/else语句列出理解循环,python-3.x,for-loop,if-statement,list-comprehension,Python 3.x,For Loop,If Statement,List Comprehension,下面的代码段可以正常工作,它产生了我想要的东西,但是我需要一些关于如何更符合Python的指示 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", "Ni

下面的代码段可以正常工作,它产生了我想要的东西,但是我需要一些关于如何更符合Python的指示

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表示shift\u pattern\u 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
):

我建议做一个小小的改变——用
not
X替换X
==0
,这可能是个好主意(那么你也可以做
not any(ava……varValue for ti…)
,但不是严格等价的。谢谢@L3viathan。它工作得很好。这就是我所缺少的。我仍然是一个初学者,今天我肯定学到了一些东西。