因此,我想创建一个while循环,一旦二维列表中的所有布尔值都是真的(特别是字典),它就会停止。
这是我要查询的字典。
tasks = {'bathroom':
{'linus':False,
'ola': False,
'lex': False,
'lotte': False,
'yoan': False,
'daan': False},
'kitchen':
{'linus':False,
'ola': False,
'lex': False,
'lotte': False,
'yoan': False,
'daan': False},
'garbage':
{'linus':False,
'ola': False,
'lex': False,
'lotte': False,
'yoan': False,
'daan': False},
'recycling':
{'linus':False,
'ola': False,
'lex': False,
'lotte': False,
'yoan': False,
'daan': False},
'corridors':
{'linus':False,
'ola': False,
'lex': False,
'lotte': False,
'yoan': False,
'daan': False}}这就是我想想出来的,但似乎行不通
while not all(done == true for done in names.values() for names in tasks.values())发布于 2021-05-14 12:59:38
He3lixxx所说的是正确的,理解是错误的。
while not all(done for names in tasks.values() for done in names.values())从一般Python工作流的角度来考虑它,只是值位于顶部,而不是嵌套循环中。
while not all(done # value
for names in tasks.values() # outer loop
for done in names.values() # inner loop
)但是,如果所有值实际上都是布尔值,则可以将其缩短为
while not all(all(task.values()) for task in tasks.values())如果你要写出第二种方法,它会
while not all(all(subtask for subtask in task.values()) for task in tasks.values())这类似于您的排序,但您必须记住all(...)部分是一个值。所以你只做“值-外部循环”,不管“值”是否是另一个理解列表本身。
发布于 2021-05-14 13:44:25
你好,我把你的代码粘贴到ide中,得到了一些错误,我发现的第一个问题是,您的真正布尔值并不适合使用大写字母T,所以它不是" true“而是"True",我看到的第二个问题是,您试图用1行with循环来解决这个问题,而循环通常需要一个":”,不能在一行中使用,遗憾的是,我没有找到一种方法来修改您的数据,但是我找到了一种访问它的方法,下面是您以后可以删除注释的代码:
finished = True
while finished:
'''
access the first dict that is the task's dict
aka kitchen, corridors, bathroom, ect, but it haven't find the names
'''
# this comment on top of this comment is for the first for loop
for task in tasks.values():
'''
find the names in every task and prints them,
you can change the print statement to a line that mutates the data
'''
# this comment is for the second for loop
for names in task.values():
print(names) # prints the data that got access, you can change this later
print(tasks) # prints the whole dict
finished = False希望我帮了你一点忙,并删除了访问数据^w^的麻烦。
https://stackoverflow.com/questions/67534581
复制相似问题