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

Esteban Kuber ekuber en gmail.com
Lun Ago 23 17:40:05 ART 2010


>>> def a():
...     for x in range(10):
...             yield x
...
>>> a()
<generator object a at 0x10048a140>
>>> list(a())
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> list(a())[0:4]
[0, 1, 2, 3]
>>>


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
>
> _______________________________________________
> pyar mailing list pyar en python.org.ar
> http://listas.python.org.ar/listinfo/pyar
>
> PyAr - Python Argentina - Sitio web: http://www.python.org.ar/
>



More information about the pyar mailing list