Path: csiph.com!usenet.pasdenom.info!news.albasani.net!newsfeed.freenet.ag!news2.euro.net!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.009 X-Spam-Evidence: '*H*': 0.98; '*S*': 0.00; 'else:': 0.03; '[0,': 0.09; '[1,': 0.09; 'res': 0.09; 'subject:howto': 0.09; 'def': 0.10; '[2,': 0.16; 'idx': 0.16; 'iterable': 0.16; 'nesting': 0.16; 'xrange': 0.16; 'wrote:': 0.17; 'drawing': 0.17; 'import': 0.21; 'fairly': 0.21; 'skip:_ 20': 0.22; 'seems': 0.23; 'raise': 0.24; 'header:In-Reply-To:1': 0.25; 'header:User-Agent:1': 0.26; 'looks': 0.26; 'values': 0.26; 'candidate': 0.26; '(e.g.,': 0.27; 'skip:_ 10': 0.29; 'class': 0.29; "i'm": 0.29; 'maybe': 0.29; 'basic': 0.30; 'code': 0.31; 'print': 0.32; 'like:': 0.33; 'to:addr:python-list': 0.33; 'self': 0.34; 'so,': 0.35; 'something': 0.35; 'add': 0.36; 'but': 0.36; 'should': 0.36; 'level': 0.37; 'subject:: ': 0.38; 'to:addr:python.org': 0.39; 'received:192': 0.39; 'received:192.168': 0.40; 'your': 0.60; 'range': 0.60; 'here': 0.65; 'levels': 0.66; 'sounds': 0.71; 'received:204': 0.72; 'self.n': 0.84; 'subject:handle': 0.84 Date: Fri, 28 Sep 2012 17:04:38 +0200 From: Laszlo Nagy User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:15.0) Gecko/20120827 Thunderbird/15.0 MIME-Version: 1.0 To: python-list@python.org Subject: Re: howto handle nested for References: In-Reply-To: Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 63 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1348844689 news.xs4all.nl 6935 [2001:888:2000:d::a6]:53203 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:30397 On 2012-09-28 16:39, Neal Becker wrote: > I know this should be a fairly basic question, but I'm drawing a blank. > > I have code that looks like: > > for s0 in xrange (n_syms): > for s1 in xrange (n_syms): > for s2 in xrange (n_syms): > for s3 in xrange (n_syms): > for s4 in range (n_syms): > for s5 in range (n_syms): > > Now I need the level of nesting to vary dynamically. (e.g., maybe I need to add > for s6 in range (n_syms)) > > Smells like a candidate for recursion. Also sounds like a use for yield. Any > suggestions? > In your example, it seem that the iterable of the for loop is always the same: range(n_sysms). It seems to be a number. Is that true? If that is so, then here is something useful: import copy class MultiLevelIterator(object): def __init__(self,levels,n): assert(levels>0) assert(n>0) self.levels = levels self.values = [0]*levels self.n = n def __iter__(self): return self def next(self): res = copy.copy(self.values) idx = self.levels-1 while idx>=0: self.values[idx]+=1 if self.values[idx]>=self.n: self.values[idx] = 0 idx-=1 else: return res raise StopIteration i = MultiLevelIterator(2,3) for values in i: print values This will print: [0, 0] [0, 1] [0, 2] [1, 0] [1, 1] [1, 2] [2, 0] [2, 1]