looking for a neat solution to a nested loop problem
Grant Edwards
invalid at invalid.invalid
Mon Aug 6 15:22:55 EDT 2012
On 2012-08-06, Tom P <werotizy at freent.dd> wrote:
>>>> no, I meant something else ..
>>>>
>>>> j runs through range(M, 100) and then range(0,M), and i runs through
>>>> range(N,100) and then range(0,N)
>>>
>>> In 2.x:
>>>
>>> for i in range(M,100)+range(0,M):
>>> for j in range(N,100)+range(0,N):
>>> do_something(i,j)
>>>
>>> Dunno if that still works in 3.x. I doubt it, since I think in 3.x
>>> range returns an iterator, not?
>>
>> Indeed it doesn't work in 3.x, but this does:
>>
>> from itertools import chain
>>
>> for i in chain(range(M,100),range(0,M)):
>> for j in chain(range(N,100),range(0,N)):
>> do_something(i,j)
>
> ah, that looks good - I guess it works in 2.x as well?
I don't know. Let me test that for you...
$ python
Python 2.6.8 (unknown, May 18 2012, 11:56:26)
[GCC 4.5.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> from itertools import chain
>>> for i in chain(range(0,5),range(5,10)):
... print i
...
0
1
2
3
4
5
6
7
8
9
>>>
Yes, it works in 2.x as well.
--
Grant Edwards grant.b.edwards Yow! ... bleakness
at ... desolation ... plastic
gmail.com forks ...
More information about the Python-list
mailing list