CKTIS / Python
itertools.dropwhile
Pomija początkowe elementy, dla których predykat jest prawdziwy.
Wersja: Python 3.14; repository image, production runtime not attested
Składnia
itertools.dropwhile(predicate, iterable, /)Pomija początkowe elementy, dla których predykat jest prawdziwy.
Test n < 3 odrzuca 1 i 2, ale na 4 przestaje działać. Zwraca 4 oraz końcowe 1 bez dalszego testowania.
Gdy bool jest prawdziwe dla wszystkich elementów, wynik jest pusty.
import itertools as I
assert list(I.dropwhile(lambda n: n < 3, [1, 2, 4, 1])) == [4, 1]
assert list(I.dropwhile(bool, [1, 2])) == []
Przykłady
import itertools as I
print(list(I.dropwhile(lambda n: n < 3, [1, 2, 4, 1])))
print(list(I.dropwhile(bool, [1, 2])))
Wynik
[4, 1] []
Gdy bool jest prawdziwe dla wszystkich elementów, wynik jest pusty.