Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #30406

Re: howto handle nested for

Path csiph.com!usenet.pasdenom.info!news.albasani.net!news.musoftware.de!wum.musoftware.de!de-l.enfer-du-nord.net!feeder2.enfer-du-nord.net!newsfeed.eweka.nl!eweka.nl!feeder3.eweka.nl!newsfeed.xs4all.nl!newsfeed6.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail
Return-Path <ian.g.kelly@gmail.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.050
X-Spam-Evidence '*H*': 0.90; '*S*': 0.00; 'sep': 0.09; 'subject:howto': 0.09; 'nesting': 0.16; 'xrange': 0.16; 'wrote:': 0.17; 'drawing': 0.17; 'fairly': 0.21; 'combination': 0.22; 'received:mail-bk0-f46.google.com': 0.22; 'cheers,': 0.23; 'header :In-Reply-To:1': 0.25; 'looks': 0.26; 'candidate': 0.26; 'am,': 0.27; '(e.g.,': 0.27; 'received:209.85.214.46': 0.27; 'message- id:@mail.gmail.com': 0.27; "i'm": 0.29; 'maybe': 0.29; 'fri,': 0.30; 'basic': 0.30; 'stuff': 0.30; 'code': 0.31; 'like:': 0.33; 'to:addr:python-list': 0.33; 'received:google.com': 0.34; 'received:209.85': 0.35; 'add': 0.36; 'but': 0.36; 'should': 0.36; 'level': 0.37; 'received:209': 0.37; 'subject:: ': 0.38; 'to:addr:python.org': 0.39; 'received:209.85.214': 0.39; 'header:Received:5': 0.40; 'range': 0.60; 'levels': 0.66; 'sounds': 0.71; 'subject:handle': 0.84; 'to:name:python': 0.84
DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :content-type; bh=7HxSJZyppq5v0/m2d+TvjJ66JfStF2xJjFrgMnjs4/o=; b=cfk0J4fVbHSqXjbm6yxreNyduiOKNutsbIDo8rcgE1YNw17lVBtz/FD7Gl1fg6YGPJ 2ekRAOhYEmNUobEA/7Ffo01uiii+yx6YE7ydPxZwnpIJzFyQzr+2gCenFLBt+SDqVb4B pPiT6VqOtWYuZXg/G4+Ln0D7bGJu5s3kqrk2umdVTTF3ehw1Y3dUNYIxwBu0uFeMK3K2 DSrk2q/0xM5iHQEpn0WayAgjmLa7Ib5Yk+lbE+hg1VGzAmUZY2vyIT4a+bp/jpmFEkdZ HkU6VoHvpRhwfXOXthjlUGwHwotZK6UjKaC14zamZANKrBjf2M1j7kwMXv49w8XyU+4a ZAmQ==
MIME-Version 1.0
In-Reply-To <k44cr4$jtj$1@ger.gmane.org>
References <k44cr4$jtj$1@ger.gmane.org>
From Ian Kelly <ian.g.kelly@gmail.com>
Date Fri, 28 Sep 2012 09:49:36 -0600
Subject Re: howto handle nested for
To Python <python-list@python.org>
Content-Type text/plain; charset=ISO-8859-1
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.15
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <http://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.1565.1348847408.27098.python-list@python.org> (permalink)
Lines 24
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1348847408 news.xs4all.nl 6861 [2001:888:2000:d::a6]:54301
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:30406

Show key headers only | View raw


On Fri, Sep 28, 2012 at 8:39 AM, Neal Becker <ndbecker2@gmail.com> 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?

levels = 6
for combination in itertools.product(xrange(n_syms), levels):
    # do stuff

Cheers,
Ian

Back to comp.lang.python | Previous | NextNext in thread | Find similar | Unroll thread


Thread

Re: howto handle nested for Ian Kelly <ian.g.kelly@gmail.com> - 2012-09-28 09:49 -0600
  Re: howto handle nested for Peter Pearson <ppearson@nowhere.invalid> - 2012-09-29 01:15 +0000
    Re: howto handle nested for Hans Mulder <hansmu@xs4all.nl> - 2012-09-29 14:41 +0200

csiph-web