社区所有版块导航
Python
python开源   Django   Python   DjangoApp   pycharm  
DATA
docker   Elasticsearch  
aigc
aigc   chatgpt  
WEB开发
linux   MongoDB   Redis   DATABASE   NGINX   其他Web框架   web工具   zookeeper   tornado   NoSql   Bootstrap   js   peewee   Git   bottle   IE   MQ   Jquery  
机器学习
机器学习算法  
Python88.com
反馈   公告   社区推广  
产品
短视频  
印度
印度  
私信  •  关注

ilamaaa

ilamaaa 最近创建的主题
ilamaaa 最近回复了
6 年前
回复了 ilamaaa 创建的主题 » Python生成器表达式:如何从循环中添加和条件?

我用列表理解来应用构建生成器的条件,用另一种方法来做,感觉不对。不管怎样,不确定这是不是你想要的。

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)])