[pyar] Cómo obtenes N elemento de un generador?

Juan Pedro Fisanotti fisadev en gmail.com
Lun Ago 23 17:46:06 ART 2010


2010/8/23 Juanjo Conti <jjconti en gmail.com>:
> Les dejo mi intento infructífero; cómo se hace?
>
>>>> def a():
> ...     for x in range(10):
> ...             yield x
> ...
>>>> a()
> <generator object a at 0xb76c89b4>
>>>> c = a()
>>>> c[0:4]
> Traceback (most recent call last):
>   File "<stdin>", line 1, in <module>
> TypeError: 'generator' object is unsubscriptable
>>>> c.next()
> 0
>>>> c.next()
> 1
>>>> c.next()
> 2
>
> --
> Juanjo Conti
> blog: http://www.juanjoconti.com.ar
>

En este post de Stack Overflow hay una solucion interesante que use hace poco:
http://stackoverflow.com/questions/2322642/index-and-slice-a-generator-in-python


-- 
fisa  -  Juan Pedro Fisanotti



More information about the pyar mailing list