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


Groups > comp.lang.python > #30406 > unrolled thread

Re: howto handle nested for

Started byIan Kelly <ian.g.kelly@gmail.com>
First post2012-09-28 09:49 -0600
Last post2012-09-29 14:41 +0200
Articles 3 — 3 participants

Back to article view | Back to comp.lang.python

This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by below is the oldest one visible, not the original post.


Contents

  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

#30406 — Re: howto handle nested for

FromIan Kelly <ian.g.kelly@gmail.com>
Date2012-09-28 09:49 -0600
SubjectRe: howto handle nested for
Message-ID<mailman.1565.1348847408.27098.python-list@python.org>
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

[toc] | [next] | [standalone]


#30448

FromPeter Pearson <ppearson@nowhere.invalid>
Date2012-09-29 01:15 +0000
Message-ID<acn0dcF4qamU1@mid.individual.net>
In reply to#30406
On Fri, 28 Sep 2012 09:49:36 -0600, Ian Kelly <ian.g.kelly@gmail.com> wrote:
>
> levels = 6
> for combination in itertools.product(xrange(n_syms), levels):
>     # do stuff

>>> n_syms = 3
>>> levels = 6
>>> for combination in itertools.product(xrange(n_syms), levels):
...   print combination
... 
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'int' object is not iterable


-- 
To email me, substitute nowhere->spamcop, invalid->net.

[toc] | [prev] | [next] | [standalone]


#30480

FromHans Mulder <hansmu@xs4all.nl>
Date2012-09-29 14:41 +0200
Message-ID<5066ec85$0$6970$e4fe514c@news2.news.xs4all.nl>
In reply to#30448
On 29/09/12 03:15:24, Peter Pearson wrote:
> On Fri, 28 Sep 2012 09:49:36 -0600, Ian Kelly <ian.g.kelly@gmail.com> wrote:
>>
>> levels = 6
>> for combination in itertools.product(xrange(n_syms), levels):
>>     # do stuff
> 
>>>> n_syms = 3
>>>> levels = 6
>>>> for combination in itertools.product(xrange(n_syms), levels):
> ...   print combination
> ... 
> Traceback (most recent call last):
>   File "<stdin>", line 1, in <module>
> TypeError: 'int' object is not iterable

>>> n_syms = 3
>>> levels = 6
>>> for combination in itertools.product(xrange(n_syms), repeat=levels):
...     print combination
...
(0, 0, 0, 0, 0, 0)
(0, 0, 0, 0, 0, 1)
(0, 0, 0, 0, 0, 2)
(0, 0, 0, 0, 1, 0)
(0, 0, 0, 0, 1, 1)
(0, 0, 0, 0, 1, 2)
(0, 0, 0, 0, 2, 0)
(0, 0, 0, 0, 2, 1)
(0, 0, 0, 0, 2, 2)
(0, 0, 0, 1, 0, 0)

etc.


Hope this helps,

-- HansM


[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web