while expression feature proposal

Paul Rubin no.email at nospam.invalid
Wed Oct 24 19:08:21 EDT 2012


Ian Kelly <ian.g.kelly at gmail.com> writes:
> j = int(random() * n)
> while j in selected:
>     j = int(random() * n)

from itertools import dropwhile

j = dropwhile(lambda j: j in selected,
                 iter(lambda: int(random() * n), object()))
             .next()

kind of ugly, makes me wish for a few more itertools primitives, but I
think it expresses reasonably directly what you are trying to do.



More information about the Python-list mailing list