, any True n -times:
def check(it, num):
it = iter(it)
return all(any(it) for _ in range(num))
>>> check([1, 1, 0], 2)
True
>>> check([1, 1, 0], 3)
False
, , , any , . all, , any False.
, , . .
, map itertools.repeat :
from itertools import repeat
def check_map(it, num):
return all(map(any, repeat(iter(it), num)))
:
# Second "True" element is in the last place
lst = [1] + [0]*1000 + [1]
%timeit check_map(lst, 2)
%timeit check(lst, 2)
%timeit many(lst, 2)
%timeit sum(l) >= 2
# Second "True" element is the second item in the iterable
lst = [1, 1] + [0]*1000
%timeit check_map(lst, 2)
%timeit check(lst, 2)
%timeit many(lst, 2)
%timeit sum(lst) >= 2