简体   繁体   中英

How to write a generator that returns ALL-BUT-LAST items in the iterable in Python?

I asked some similar questions [1, 2] yesterday and got great answers, but I am not yet technically skilled enough to write a generator of such sophistication myself.

How could I write a generator that would raise StopIteration if it's the last item, instead of yielding it?

I am thinking I should somehow ask two values at a time, and see if the 2nd value is StopIteration. If it is, then instead of yielding the first value, I should raise this StopIteration. But somehow I should also remember the 2nd value that I asked if it wasn't StopIteration.

I don't know how to write it myself. Please help.

For example, if the iterable is [1, 2, 3], then the generator should return 1 and 2.

Thanks, Boda Cydo.

[1] How do I modify a generator in Python?

[2] How to determine if the value is ONE-BUT-LAST in a Python generator?

This should do the trick:

def allbutlast(iterable):
    it = iter(iterable)
    current = it.next()
    for i in it:
        yield current
        current = i


>>> list(allbutlast([1,2,3]))
[1, 2]

This will iterate through the entire list, and return the previous item so the last item is never returned.
Note that calling the above on both [] and [1] will return an empty list.

First off, is a generator really needed? This sounds like the perfect job for Python's slices syntax :

result = my_range[ : -1]

Ie: take a range form the first item to the one before the last.

the itertools module shows a pairwise() method in its recipes. adapting from this recipe, you can get your generator:

from itertools import *

def n_apart(iterable, n):
    a,b = tee(iterable)
    for count in range(n):
        next(b)
    return zip(a,b)

def all_but_n_last(iterable, n):
    return (value for value,dummy in n_apart(iterable, n))

the n_apart() function return pairs of values which are n elements apart in the input iterable, ignoring all pairs . all_but_b_last() returns the first value of all pairs, which incidentally ignores the n last elements of the list.

>>> data = range(10)
>>> list(data)
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> list(n_apart(data,3))
[(0, 3), (1, 4), (2, 5), (3, 6), (4, 7), (5, 8), (6, 9)]
>>> list(all_but_n_last(data,3))
[0, 1, 2, 3, 4, 5, 6]
>>> 
>>> list(all_but_n_last(data,1))
[0, 1, 2, 3, 4, 5, 6, 7, 8]

The more_itertools project has a tool that emulates itertools.islice with support for negative indices:

import more_itertools as mit

list(mit.islice_extended([1, 2, 3], None, -1))
# [1, 2]
gen = (x for x in iterable[:-1])

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM