小编The*_*kal的帖子

使用列表理解加速“any”

我正在使用any列表理解。我想在any返回时打破列表理解True。例如,

import time

def f(x):
    time.sleep(2)
    return x

beginTime = time.time()
result = any([f(x) == 0 for x in [0,1,3,5,7]])
endTime = time.time()

print(endTime - beginTime)
Run Code Online (Sandbox Code Playgroud)

上面的代码打印了10 秒,尽管它可能会在 first 之后中断迭代True

python performance list-comprehension any

1
推荐指数
1
解决办法
82
查看次数

标签 统计

any ×1

list-comprehension ×1

performance ×1

python ×1