mult-indexed xrange?
Huaiyu Zhu
hzhu at mars.localdomain
Tue Mar 5 03:55:52 EST 2002
On Mon, 4 Mar 2002 22:06:24 -0600, Jason Orendorff <jason at jorendorff.com> wrote:
>Huaiyu Zhu wrote:
>> I forgot to mention: without generating temporary lists.
>>
>> >
>> >for (x,y) in [(i,j) for i in range(2) for j in range(3) ]:
>> > print x,y
>> >0 0 0
>> >0 0 1
>> ...
>>
>> Is list comprehension lazy, ie. without generating the actual list?
>
>No, list comprehensions are eager.
>
>You want generators:
>
>from __future__ import generators
>
>def pairs(n, m):
> for i in xrange(n):
> for j in xrange(m):
> yield (i, j)
>
>for (x, y) in pairs(2, 3):
> print x, y
>
>## Jason Orendorff http://www.jorendorff.com/
>
Well, that was what I started with. :-)
I gave a more general solution, which could be an extension of xrange to
multiple indices. I was wondering if there is a builtin facility for this.
Looks like there isn't.
Huaiyu
More information about the Python-list
mailing list