Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!news.glorb.com!news-out.readnews.com!transit3.readnews.com!panix!not-for-mail From: Grant Edwards Newsgroups: comp.lang.python Subject: Re: looking for a neat solution to a nested loop problem Date: Mon, 6 Aug 2012 19:22:55 +0000 (UTC) Organization: PANIX Public Access Internet and UNIX, NYC Lines: 54 Message-ID: References: NNTP-Posting-Host: dsl.comtrol.com X-Trace: reader1.panix.com 1344280975 22650 64.122.56.22 (6 Aug 2012 19:22:55 GMT) X-Complaints-To: abuse@panix.com NNTP-Posting-Date: Mon, 6 Aug 2012 19:22:55 +0000 (UTC) User-Agent: slrn/pre1.0.0-18 (Linux) Xref: csiph.com comp.lang.python:26649 On 2012-08-06, Tom P 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 ...