Path: csiph.com!newsfeed.hal-mli.net!feeder3.hal-mli.net!newsfeed.hal-mli.net!feeder1.hal-mli.net!news.linkpendium.com!news.linkpendium.com!panix!gordon From: John Gordon Newsgroups: comp.lang.python Subject: Re: looking for a neat solution to a nested loop problem Date: Mon, 6 Aug 2012 16:03:50 +0000 (UTC) Organization: PANIX Public Access Internet and UNIX, NYC Lines: 31 Message-ID: References: NNTP-Posting-Host: panix3.panix.com X-Trace: reader1.panix.com 1344269030 16265 166.84.1.3 (6 Aug 2012 16:03:50 GMT) X-Complaints-To: abuse@panix.com NNTP-Posting-Date: Mon, 6 Aug 2012 16:03:50 +0000 (UTC) User-Agent: nn/6.7.3 Xref: csiph.com comp.lang.python:26627 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 -- 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"