Path: csiph.com!usenet.pasdenom.info!weretis.net!feeder4.news.weretis.net!news.musoftware.de!wum.musoftware.de!fu-berlin.de!uni-berlin.de!individual.net!not-for-mail From: Tom P Newsgroups: comp.lang.python Subject: Re: looking for a neat solution to a nested loop problem Date: Mon, 06 Aug 2012 19:16:45 +0200 Lines: 30 Message-ID: References: Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-Trace: individual.net JuLC5B1KZKm3vk4lv8870AUlDPGvl/Cq4csIXIvHeqJ6lkxI2bIWIslSVJtw08LSw= Cancel-Lock: sha1:NBG6auXAhkHCfe+UH2saChdWT7c= User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:14.0) Gecko/20120714 Thunderbird/14.0 In-Reply-To: Xref: csiph.com comp.lang.python:26641 On 08/06/2012 06:03 PM, John Gordon wrote: > In Tom P 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.