我用列表理解来应用构建生成器的条件,用另一种方法来做,感觉不对。不管怎样,不确定这是不是你想要的。
x = ([second[0] <= 1 and second[1] <=2 for second in first] for first in dist)
for y in x:
print(y)
输出:
[True, False, True, True, False, False, False]
[False, True, False, False, False, False, False]
[True, False, True, False, False, False, False]
[True, False, False, True, False, False, False]
[False, False, False, False, True, False, False]
[False, False, False, False, False, True, False]
[False, False, False, False, False, False, True]
对于你想要的版本,感觉不对
x = ((second[0] <= 1 and second[1] <=2 for second in first) for first in dist)
for y in x:
print([next(y),next(y),next(y),next(y),next(y),next(y),next(y)])
输出相同
现在编写一个单独的函数来进行比较
def compare(a,b):
return all([a[i] <= b[i] for i in range(len(a))])
x = ((compare(second, [1,2]) for second in first) for first in dist)
for y in x:
print([next(y),next(y),next(y),next(y),next(y),next(y),next(y)])