[Python] Skipping rows in lazy generator chaining?

Discussão em 'Python' iniciado por Stack, Fevereiro 19, 2025.

  1. Stack

    Stack Membro Participativo

    I have a lazy chain of generators. I would like to chain a generator onto the output of these, but only onto a "range" subset of the output. Specifically, I would like to skip some rows:

    def foo():
    yield "pickles"
    yield from iter(range(4))

    def bar(foo_numbers):
    yield from (5*num for num in foo_numbers)

    # how to construct foo_bar from `foo` and `bar`?

    assert foo_bar == ['pickles', 0, 5, 10, 15]


    Notice that the output foo_bar can be obtained from foo followed by bar, except bar should only apply to the first through nth element of the output of foo (skipping the zeroth element). Is there an easy way to have my generators skip n elements?

    Continue reading...

Compartilhe esta Página