Re: howto handle nested for

Discussion in 'Python' started by Ian Kelly, Sep 28, 2012.

  1. Ian Kelly

    Ian Kelly Guest

    On Fri, Sep 28, 2012 at 8:39 AM, 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?


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

    Cheers,
    Ian
    Ian Kelly, Sep 28, 2012
    #1
    1. Advertising

  2. On Fri, 28 Sep 2012 09:49:36 -0600, Ian Kelly <> 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.
    Peter Pearson, Sep 29, 2012
    #2
    1. Advertising

  3. Ian Kelly

    Hans Mulder Guest

    On 29/09/12 03:15:24, Peter Pearson wrote:
    > On Fri, 28 Sep 2012 09:49:36 -0600, Ian Kelly <> 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
    Hans Mulder, Sep 29, 2012
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Neal Becker

    howto handle nested for

    Neal Becker, Sep 28, 2012, in forum: Python
    Replies:
    1
    Views:
    139
    Wojtek
    Sep 28, 2012
  2. Laszlo Nagy

    Re: howto handle nested for

    Laszlo Nagy, Sep 28, 2012, in forum: Python
    Replies:
    1
    Views:
    148
    Neil Cerutti
    Sep 28, 2012
  3. Tim Chase

    Re: howto handle nested for

    Tim Chase, Sep 28, 2012, in forum: Python
    Replies:
    0
    Views:
    127
    Tim Chase
    Sep 28, 2012
  4. Neal Becker

    Re: howto handle nested for

    Neal Becker, Sep 28, 2012, in forum: Python
    Replies:
    0
    Views:
    135
    Neal Becker
    Sep 28, 2012
  5. Peter Otten

    Re: howto handle nested for

    Peter Otten, Sep 28, 2012, in forum: Python
    Replies:
    0
    Views:
    129
    Peter Otten
    Sep 28, 2012
Loading...

Share This Page