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


Groups > comp.lang.python > #30406

Re: howto handle nested for

References <k44cr4$jtj$1@ger.gmane.org>
From Ian Kelly <ian.g.kelly@gmail.com>
Date 2012-09-28 09:49 -0600
Subject Re: howto handle nested for
Newsgroups comp.lang.python
Message-ID <mailman.1565.1348847408.27098.python-list@python.org> (permalink)

Show all headers | 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