Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #26625 > unrolled thread
| Started by | Tom P <werotizy@freent.dd> |
|---|---|
| First post | 2012-08-06 17:52 +0200 |
| Last post | 2012-08-06 21:14 +0100 |
| Articles | 20 on this page of 23 — 13 participants |
Back to article view | Back to comp.lang.python
looking for a neat solution to a nested loop problem Tom P <werotizy@freent.dd> - 2012-08-06 17:52 +0200
Re: looking for a neat solution to a nested loop problem John Gordon <gordon@panix.com> - 2012-08-06 16:03 +0000
Re: looking for a neat solution to a nested loop problem Tom P <werotizy@freent.dd> - 2012-08-06 19:16 +0200
Re: looking for a neat solution to a nested loop problem Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2012-08-07 01:27 +0000
Re: looking for a neat solution to a nested loop problem Ian Foote <ian@feete.org> - 2012-08-06 17:07 +0100
Re: looking for a neat solution to a nested loop problem Ethan Furman <ethan@stoneleaf.us> - 2012-08-06 09:15 -0700
Re: looking for a neat solution to a nested loop problem Nobody <nobody@nowhere.com> - 2012-08-06 17:18 +0100
Re: looking for a neat solution to a nested loop problem Tom P <werotizy@freent.dd> - 2012-08-06 19:14 +0200
Re: looking for a neat solution to a nested loop problem Emile van Sebille <emile@fenx.com> - 2012-08-06 11:11 -0700
Re: looking for a neat solution to a nested loop problem Larry Hudson <orgnut@yahoo.com> - 2012-08-06 21:02 -0700
Re: looking for a neat solution to a nested loop problem Nobody <nobody@nowhere.com> - 2012-08-07 16:32 +0100
Re: looking for a neat solution to a nested loop problem 88888 Dihedral <dihedral88888@googlemail.com> - 2012-08-07 18:42 -0700
Re: looking for a neat solution to a nested loop problem 88888 Dihedral <dihedral88888@googlemail.com> - 2012-08-09 11:46 -0700
Re: looking for a neat solution to a nested loop problem 88888 Dihedral <dihedral88888@googlemail.com> - 2012-08-09 11:39 -0700
Re: looking for a neat solution to a nested loop problem 88888 Dihedral <dihedral88888@googlemail.com> - 2012-08-14 14:08 -0700
Re: looking for a neat solution to a nested loop problem Grant Edwards <invalid@invalid.invalid> - 2012-08-06 18:25 +0000
Re: looking for a neat solution to a nested loop problem Grant Edwards <invalid@invalid.invalid> - 2012-08-06 18:29 +0000
Re: looking for a neat solution to a nested loop problem Tom P <werotizy@freent.dd> - 2012-08-06 21:03 +0200
Re: looking for a neat solution to a nested loop problem Grant Edwards <invalid@invalid.invalid> - 2012-08-06 19:22 +0000
Re: looking for a neat solution to a nested loop problem Emile van Sebille <emile@fenx.com> - 2012-08-06 12:52 -0700
Re: looking for a neat solution to a nested loop problem André Malo <ndparker@gmail.com> - 2012-08-06 20:19 +0200
Re: looking for a neat solution to a nested loop problem Dennis Lee Bieber <wlfraed@ix.netcom.com> - 2012-08-06 15:31 -0400
Re: looking for a neat solution to a nested loop problem Arnaud Delobelle <arnodel@gmail.com> - 2012-08-06 21:14 +0100
Page 1 of 2 [1] 2 Next page →
| From | Tom P <werotizy@freent.dd> |
|---|---|
| Date | 2012-08-06 17:52 +0200 |
| Subject | looking for a neat solution to a nested loop problem |
| Message-ID | <a8a7hvF8crU1@mid.individual.net> |
consider a nested loop algorithm -
for i in range(100):
for j in range(100):
do_something(i,j)
Now, suppose I don't want to use i = 0 and j = 0 as initial values, but
some other values i = N and j = M, and I want to iterate through all
10,000 values in sequence - is there a neat python-like way to this? I
realize I can do things like use a variable for k in range(10000): and
then derive values for i and j from k, but I'm wondering if there's
something less clunky.
[toc] | [next] | [standalone]
| From | John Gordon <gordon@panix.com> |
|---|---|
| Date | 2012-08-06 16:03 +0000 |
| Message-ID | <jvopt5$fs9$1@reader1.panix.com> |
| In reply to | #26625 |
In <a8a7hvF8crU1@mid.individual.net> Tom P <werotizy@freent.dd> writes:
> consider a nested loop algorithm -
> for i in range(100):
> for j in range(100):
> do_something(i,j)
> Now, suppose I don't want to use i = 0 and j = 0 as initial values, but
> some other values i = N and j = M, and I want to iterate through all
> 10,000 values in sequence - is there a neat python-like way to this? I
> realize I can do things like use a variable for k in range(10000): and
> then derive values for i and j from k, but I'm wondering if there's
> something less clunky.
You could define your own generator function that yields values
in whatever order you want:
def my_generator():
yield 9
yield 100
for i in range(200, 250):
yield i
yield 5
--
John Gordon A is for Amy, who fell down the stairs
gordon@panix.com B is for Basil, assaulted by bears
-- Edward Gorey, "The Gashlycrumb Tinies"
[toc] | [prev] | [next] | [standalone]
| From | Tom P <werotizy@freent.dd> |
|---|---|
| Date | 2012-08-06 19:16 +0200 |
| Message-ID | <a8acftFcukU2@mid.individual.net> |
| In reply to | #26627 |
On 08/06/2012 06:03 PM, John Gordon wrote: > In <a8a7hvF8crU1@mid.individual.net> Tom P <werotizy@freent.dd> writes: > >> consider a nested loop algorithm - > >> for i in range(100): >> for j in range(100): >> do_something(i,j) > >> Now, suppose I don't want to use i = 0 and j = 0 as initial values, but >> some other values i = N and j = M, and I want to iterate through all >> 10,000 values in sequence - is there a neat python-like way to this? I >> realize I can do things like use a variable for k in range(10000): and >> then derive values for i and j from k, but I'm wondering if there's >> something less clunky. > > You could define your own generator function that yields values > in whatever order you want: > > def my_generator(): > yield 9 > yield 100 > for i in range(200, 250): > yield i > yield 5 > > Thanks, I'll look at that but I think it just moves the clunkiness from one place in the code to another.
[toc] | [prev] | [next] | [standalone]
| From | Steven D'Aprano <steve+comp.lang.python@pearwood.info> |
|---|---|
| Date | 2012-08-07 01:27 +0000 |
| Message-ID | <50206eeb$0$29978$c3e8da3$5496439d@news.astraweb.com> |
| In reply to | #26641 |
On Mon, 06 Aug 2012 19:16:45 +0200, Tom P wrote:
>> def my_generator():
>> yield 9
>> yield 100
>> for i in range(200, 250):
>> yield i
>> yield 5
>>
>>
> Thanks, I'll look at that but I think it just moves the clunkiness from
> one place in the code to another.
And if there was a built-in command that did exactly what you wanted, it
too would also move the clunkiness from one place in the code to another.
What you are asking for is clunky:
[quote]
j runs through range(M, 100) and then range(0,M), and
i runs through range(N,100) and then range(0,N)
[end quote]
There's no magic pixie dust that you can sprinkle on it to make it
elegant. Assuming M and N are small (under 100), you can do this:
values = range(100) # or list(range(100)) in Python 3.
for j in (values[M:] + values[:M]):
for i in (values[N:] + values[:N]):
...
which isn't too bad. If you have to deal with much large ranges, you can
use itertools to chain them together:
import itertools
jvalues = itertools.chain(xrange(M, 1000000), xrange(M)) # or just range
ivalues = itertools.chain(xrange(N, 2500000), xrange(N)) # in Python 3
for j in jvalues:
for i in ivalues:
...
--
Steven
[toc] | [prev] | [next] | [standalone]
| From | Ian Foote <ian@feete.org> |
|---|---|
| Date | 2012-08-06 17:07 +0100 |
| Message-ID | <mailman.3014.1344269252.4697.python-list@python.org> |
| In reply to | #26625 |
The function range can be called with more than one argument. For example:
for i in range(N, N + 10):
for j in range(M, M + 100):
do_something(i, j)
You can also call range with 3 arguments, if want a step size different
to 1:
for k in range(2, 11, 3):
print(k)
2
5
8
Hope this is clear,
Ian
On 06/08/12 16:52, Tom P wrote:
> consider a nested loop algorithm -
>
> for i in range(100):
> for j in range(100):
> do_something(i,j)
>
> Now, suppose I don't want to use i = 0 and j = 0 as initial values,
> but some other values i = N and j = M, and I want to iterate through
> all 10,000 values in sequence - is there a neat python-like way to
> this? I realize I can do things like use a variable for k in
> range(10000): and then derive values for i and j from k, but I'm
> wondering if there's something less clunky.
[toc] | [prev] | [next] | [standalone]
| From | Ethan Furman <ethan@stoneleaf.us> |
|---|---|
| Date | 2012-08-06 09:15 -0700 |
| Message-ID | <mailman.3015.1344269486.4697.python-list@python.org> |
| In reply to | #26625 |
Tom P wrote:
> consider a nested loop algorithm -
>
> for i in range(100):
> for j in range(100):
> do_something(i,j)
>
> Now, suppose I don't want to use i = 0 and j = 0 as initial values, but
> some other values i = N and j = M, and I want to iterate through all
> 10,000 values in sequence - is there a neat python-like way to this? I
> realize I can do things like use a variable for k in range(10000): and
> then derive values for i and j from k, but I'm wondering if there's
> something less clunky.
for i in range(N, N+100):
for j in range(M, M+100):
do_something(i, j)
~Ethan~
[toc] | [prev] | [next] | [standalone]
| From | Nobody <nobody@nowhere.com> |
|---|---|
| Date | 2012-08-06 17:18 +0100 |
| Message-ID | <pan.2012.08.06.16.18.08.86000@nowhere.com> |
| In reply to | #26625 |
On Mon, 06 Aug 2012 17:52:31 +0200, Tom P wrote: > consider a nested loop algorithm - > > for i in range(100): > for j in range(100): > do_something(i,j) > > Now, suppose I don't want to use i = 0 and j = 0 as initial values, but > some other values i = N and j = M, and I want to iterate through all > 10,000 values in sequence - is there a neat python-like way to this? for i in range(N,N+100): for j in range(M,M+100): do_something(i,j) Or did you mean something else? Alternatively: import itertools for i, j in itertools.product(range(N,N+100),range(M,M+100)): do_something(i,j) This can be preferable to deeply-nested loops. Also: in 2.x, use xrange() in preference to range().
[toc] | [prev] | [next] | [standalone]
| From | Tom P <werotizy@freent.dd> |
|---|---|
| Date | 2012-08-06 19:14 +0200 |
| Message-ID | <a8acciFcukU1@mid.individual.net> |
| In reply to | #26634 |
On 08/06/2012 06:18 PM, Nobody wrote: > On Mon, 06 Aug 2012 17:52:31 +0200, Tom P wrote: > >> consider a nested loop algorithm - >> >> for i in range(100): >> for j in range(100): >> do_something(i,j) >> >> Now, suppose I don't want to use i = 0 and j = 0 as initial values, but >> some other values i = N and j = M, and I want to iterate through all >> 10,000 values in sequence - is there a neat python-like way to this? > > for i in range(N,N+100): > for j in range(M,M+100): > do_something(i,j) > > Or did you mean something else? 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) .. apologies if I didn't make that clear enough. > > Alternatively: > > import itertools > > for i, j in itertools.product(range(N,N+100),range(M,M+100)): > do_something(i,j) > > This can be preferable to deeply-nested loops. > > Also: in 2.x, use xrange() in preference to range(). >
[toc] | [prev] | [next] | [standalone]
| From | Emile van Sebille <emile@fenx.com> |
|---|---|
| Date | 2012-08-06 11:11 -0700 |
| Message-ID | <mailman.3022.1344276685.4697.python-list@python.org> |
| In reply to | #26640 |
On 8/6/2012 10:14 AM Tom P said...
> On 08/06/2012 06:18 PM, Nobody wrote:
>> On Mon, 06 Aug 2012 17:52:31 +0200, Tom P wrote:
>>
>>> consider a nested loop algorithm -
>>>
>>> for i in range(100):
>>> for j in range(100):
>>> do_something(i,j)
>>>
>>> Now, suppose I don't want to use i = 0 and j = 0 as initial values, but
>>> some other values i = N and j = M, and I want to iterate through all
>>> 10,000 values in sequence - is there a neat python-like way to this?
>>
>> for i in range(N,N+100):
>> for j in range(M,M+100):
>> do_something(i,j)
>>
>> Or did you mean something else?
>
> 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)
>
> .. apologies if I didn't make that clear enough.
>
for i in range(N,N+100):
for j in range(M,M+100):
do_something(i % 100 ,j % 100)
Emile
[toc] | [prev] | [next] | [standalone]
| From | Larry Hudson <orgnut@yahoo.com> |
|---|---|
| Date | 2012-08-06 21:02 -0700 |
| Message-ID | <LsOdnTFsmezHDr3NnZ2dnUVZ5h6dnZ2d@giganews.com> |
| In reply to | #26643 |
On 08/06/2012 11:11 AM, Emile van Sebille wrote:
<snip>
>
> for i in range(N,N+100):
> for j in range(M,M+100):
> do_something(i % 100 ,j % 100)
>
> Emile
How about...
for i in range(100):
for j in range(100):
do_something((i + N) % 100, (j + M) % 100)
-=- Larry -=-
[toc] | [prev] | [next] | [standalone]
| From | Nobody <nobody@nowhere.com> |
|---|---|
| Date | 2012-08-07 16:32 +0100 |
| Message-ID | <pan.2012.08.07.15.32.54.912000@nowhere.com> |
| In reply to | #26678 |
On Mon, 06 Aug 2012 21:02:33 -0700, Larry Hudson wrote: >> for i in range(N,N+100): >> for j in range(M,M+100): >> do_something(i % 100 ,j % 100) >> >> Emile > > How about... > > for i in range(100): > for j in range(100): > do_something((i + N) % 100, (j + M) % 100) Both of these approaches move the modifications to the sequence into the body of the loop. It may be preferable to iterate over the desired sequence directly. E.g. for i in ((N + ii) % 100 for ii in xrange(100)): for j in ((M + jj) % 100 for jj in xrange(100)): do_something(i, j)
[toc] | [prev] | [next] | [standalone]
| From | 88888 Dihedral <dihedral88888@googlemail.com> |
|---|---|
| Date | 2012-08-07 18:42 -0700 |
| Message-ID | <e121d07b-c947-4f69-9acf-2a0bf1352f56@googlegroups.com> |
| In reply to | #26721 |
Nobody於 2012年8月7日星期二UTC+8下午11時32分55秒寫道: > On Mon, 06 Aug 2012 21:02:33 -0700, Larry Hudson wrote: > > > > >> for i in range(N,N+100): > > >> for j in range(M,M+100): > > >> do_something(i % 100 ,j % 100) > > >> > > >> Emile > > > > > > How about... > > > > > > for i in range(100): > > > for j in range(100): > > > do_something((i + N) % 100, (j + M) % 100) > > > > Both of these approaches move the modifications to the sequence into the > > body of the loop. It may be preferable to iterate over the desired > > sequence directly. E.g. > > > > for i in ((N + ii) % 100 for ii in xrange(100)): > > for j in ((M + jj) % 100 for jj in xrange(100)): > > do_something(i, j) Nobody於 2012年8月7日星期二UTC+8下午11時32分55秒寫道: > On Mon, 06 Aug 2012 21:02:33 -0700, Larry Hudson wrote: > > > > >> for i in range(N,N+100): > > >> for j in range(M,M+100): > > >> do_something(i % 100 ,j % 100) > > >> > > >> Emile > > > > > > How about... > > > > > > for i in range(100): > > > for j in range(100): > > > do_something((i + N) % 100, (j + M) % 100) > > > > Both of these approaches move the modifications to the sequence into the > > body of the loop. It may be preferable to iterate over the desired > > sequence directly. E.g. > > > > for i in ((N + ii) % 100 for ii in xrange(100)): > > for j in ((M + jj) % 100 for jj in xrange(100)): > > do_something(i, j) This is a good example to be tuned into some example such that this kind of loops by iterators of parameters in python wich do not use any division at all. But I am getting lazy recently for non-critical parts.
[toc] | [prev] | [next] | [standalone]
| From | 88888 Dihedral <dihedral88888@googlemail.com> |
|---|---|
| Date | 2012-08-09 11:46 -0700 |
| Message-ID | <fa0f7527-ac84-4d3b-aa88-27ff3c874874@googlegroups.com> |
| In reply to | #26721 |
Nobody於 2012年8月7日星期二UTC+8下午11時32分55秒寫道: > On Mon, 06 Aug 2012 21:02:33 -0700, Larry Hudson wrote: > > > > >> for i in range(N,N+100): > > >> for j in range(M,M+100): > > >> do_something(i % 100 ,j % 100) > > >> > > >> Emile > > > > > > How about... > > > > > > for i in range(100): > > > for j in range(100): > > > do_something((i + N) % 100, (j + M) % 100) > > > > Both of these approaches move the modifications to the sequence into the > > body of the loop. It may be preferable to iterate over the desired > > sequence directly. E.g. > > > > for i in ((N + ii) % 100 for ii in xrange(100)): > > for j in ((M + jj) % 100 for jj in xrange(100)): > > do_something(i, j) list(range(N,100))+ list(range(0,N)) //good for j Well, this is the way!
[toc] | [prev] | [next] | [standalone]
| From | 88888 Dihedral <dihedral88888@googlemail.com> |
|---|---|
| Date | 2012-08-09 11:39 -0700 |
| Message-ID | <6b530f50-27ff-4cf0-b5f3-dae4b9c2fded@googlegroups.com> |
| In reply to | #26721 |
Nobody於 2012年8月7日星期二UTC+8下午11時32分55秒寫道: > On Mon, 06 Aug 2012 21:02:33 -0700, Larry Hudson wrote: > > > > >> for i in range(N,N+100): > > >> for j in range(M,M+100): > > >> do_something(i % 100 ,j % 100) > > >> > > >> Emile > > > > > > How about... > > > > > > for i in range(100): > > > for j in range(100): > > > do_something((i + N) % 100, (j + M) % 100) > > > > Both of these approaches move the modifications to the sequence into the > > body of the loop. It may be preferable to iterate over the desired > > sequence directly. E.g. > > > > for i in ((N + ii) % 100 for ii in xrange(100)): > > for j in ((M + jj) % 100 for jj in xrange(100)): > > do_something(i, j) Nobody於 2012年8月7日星期二UTC+8下午11時32分55秒寫道: > On Mon, 06 Aug 2012 21:02:33 -0700, Larry Hudson wrote: > > > > >> for i in range(N,N+100): > > >> for j in range(M,M+100): > > >> do_something(i % 100 ,j % 100) > > >> > > >> Emile > > > > > > How about... > > > > > > for i in range(100): > > > for j in range(100): > > > do_something((i + N) % 100, (j + M) % 100) > > > > Both of these approaches move the modifications to the sequence into the > > body of the loop. It may be preferable to iterate over the desired > > sequence directly. E.g. > > > > for i in ((N + ii) % 100 for ii in xrange(100)): > > for j in ((M + jj) % 100 for jj in xrange(100)) list(range(N,100))+list(range(1,N)) //good for i I contribute my python code here to avoid any divison.
[toc] | [prev] | [next] | [standalone]
| From | 88888 Dihedral <dihedral88888@googlemail.com> |
|---|---|
| Date | 2012-08-14 14:08 -0700 |
| Message-ID | <0207a924-6c0c-42eb-9f1e-3a07bb3097b1@googlegroups.com> |
| In reply to | #26721 |
Nobody於 2012年8月7日星期二UTC+8下午11時32分55秒寫道:
> On Mon, 06 Aug 2012 21:02:33 -0700, Larry Hudson wrote:
>
>
>
> >> for i in range(N,N+100):
>
> >> for j in range(M,M+100):
>
> >> do_something(i % 100 ,j % 100)
>
> >>
>
> >> Emile
>
> >
>
> > How about...
>
> >
>
> > for i in range(100):
>
> > for j in range(100):
>
> > do_something((i + N) % 100, (j + M) % 100)
>
>
>
> Both of these approaches move the modifications to the sequence into the
>
> body of the loop. It may be preferable to iterate over the desired
>
> sequence directly. E.g.
>
>
>
> for i in ((N + ii) % 100 for ii in xrange(100)):
>
> for j in ((M + jj) % 100 for jj in xrange(100)):
>
> do_something(i, j)
I'll contrubute another version by the xrange function in python.
for i in xrange(N, N+100): # not list based
if i<100: i2=i else: i2=i-100 # not in the inner most loop
for j in xrange(M, M+100):
if j<100: j2=j else: j2=j-100
do_something(i2,j2)
[toc] | [prev] | [next] | [standalone]
| From | Grant Edwards <invalid@invalid.invalid> |
|---|---|
| Date | 2012-08-06 18:25 +0000 |
| Message-ID | <jvp25t$k8e$1@reader1.panix.com> |
| In reply to | #26640 |
On 2012-08-06, Tom P <werotizy@freent.dd> wrote:
> On 08/06/2012 06:18 PM, Nobody wrote:
>> On Mon, 06 Aug 2012 17:52:31 +0200, Tom P wrote:
>>
>>> consider a nested loop algorithm -
>>>
>>> for i in range(100):
>>> for j in range(100):
>>> do_something(i,j)
>>>
>>> Now, suppose I don't want to use i = 0 and j = 0 as initial values, but
>>> some other values i = N and j = M, and I want to iterate through all
>>> 10,000 values in sequence - is there a neat python-like way to this?
>>
>> for i in range(N,N+100):
>> for j in range(M,M+100):
>> do_something(i,j)
>>
>> Or did you mean something else?
>
> 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?
--
Grant Edwards grant.b.edwards Yow! I wish I was on a
at Cincinnati street corner
gmail.com holding a clean dog!
[toc] | [prev] | [next] | [standalone]
| From | Grant Edwards <invalid@invalid.invalid> |
|---|---|
| Date | 2012-08-06 18:29 +0000 |
| Message-ID | <jvp2eb$k8e$2@reader1.panix.com> |
| In reply to | #26645 |
On 2012-08-06, Grant Edwards <invalid@invalid.invalid> wrote:
> On 2012-08-06, Tom P <werotizy@freent.dd> wrote:
>> On 08/06/2012 06:18 PM, Nobody wrote:
>>> On Mon, 06 Aug 2012 17:52:31 +0200, Tom P wrote:
>>>
>>>> consider a nested loop algorithm -
>>>>
>>>> for i in range(100):
>>>> for j in range(100):
>>>> do_something(i,j)
>>>>
>>>> Now, suppose I don't want to use i = 0 and j = 0 as initial values, but
>>>> some other values i = N and j = M, and I want to iterate through all
>>>> 10,000 values in sequence - is there a neat python-like way to this?
>>>
>>> for i in range(N,N+100):
>>> for j in range(M,M+100):
>>> do_something(i,j)
>>>
>>> Or did you mean something else?
>>
>> 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)
--
Grant Edwards grant.b.edwards Yow! People humiliating
at a salami!
gmail.com
[toc] | [prev] | [next] | [standalone]
| From | Tom P <werotizy@freent.dd> |
|---|---|
| Date | 2012-08-06 21:03 +0200 |
| Message-ID | <a8aio3Ftq2U1@mid.individual.net> |
| In reply to | #26646 |
On 08/06/2012 08:29 PM, Grant Edwards wrote: > On 2012-08-06, Grant Edwards <invalid@invalid.invalid> wrote: >> On 2012-08-06, Tom P <werotizy@freent.dd> wrote: >>> On 08/06/2012 06:18 PM, Nobody wrote: >>>> On Mon, 06 Aug 2012 17:52:31 +0200, Tom P wrote: >>>> >>>>> consider a nested loop algorithm - >>>>> >>>>> for i in range(100): >>>>> for j in range(100): >>>>> do_something(i,j) >>>>> >>>>> Now, suppose I don't want to use i = 0 and j = 0 as initial values, but >>>>> some other values i = N and j = M, and I want to iterate through all >>>>> 10,000 values in sequence - is there a neat python-like way to this? >>>> >>>> for i in range(N,N+100): >>>> for j in range(M,M+100): >>>> do_something(i,j) >>>> >>>> Or did you mean something else? >>> >>> 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?
[toc] | [prev] | [next] | [standalone]
| From | Grant Edwards <invalid@invalid.invalid> |
|---|---|
| Date | 2012-08-06 19:22 +0000 |
| Message-ID | <jvp5if$m3q$1@reader1.panix.com> |
| In reply to | #26647 |
On 2012-08-06, Tom P <werotizy@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 ...
[toc] | [prev] | [next] | [standalone]
| From | Emile van Sebille <emile@fenx.com> |
|---|---|
| Date | 2012-08-06 12:52 -0700 |
| Message-ID | <mailman.3026.1344282782.4697.python-list@python.org> |
| In reply to | #26649 |
On 8/6/2012 12:22 PM Grant Edwards said... > On 2012-08-06, Tom P <werotizy@freent.dd> wrote: <snip> >> ah, that looks good - I guess it works in 2.x as well? > > I don't know. Let me test that for you... > <snip> > > Yes, it works in 2.x as well. > :) And from the docs, all the way back to 2.3! 9.7. itertools Functions creating iterators for efficient looping New in version 2.3. Emile
[toc] | [prev] | [next] | [standalone]
Page 1 of 2 [1] 2 Next page →
Back to top | Article view | comp.lang.python
csiph-web