Account Options

  1. Sign in
The old Google Groups will be going away soon, but your browser is incompatible with the new version.
Google Groups Home
« Groups Home
howto handle nested for
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  11 messages - Collapse all  -  Translate all to Translated (View all originals)
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
Neal Becker  
View profile  
 More options Sep 28 2012, 10:39 am
Newsgroups: comp.lang.python
From: Neal Becker <ndbeck...@gmail.com>
Date: Fri, 28 Sep 2012 10:39:32 -0400
Local: Fri, Sep 28 2012 10:39 am
Subject: howto handle nested for
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?


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Alister  
View profile  
 More options Sep 28 2012, 10:42 am
Newsgroups: comp.lang.python
From: Alister <alister.w...@ntlworld.com>
Date: Fri, 28 Sep 2012 14:42:20 GMT
Local: Fri, Sep 28 2012 10:42 am
Subject: Re: howto handle nested for

It definitely looks like for is the wrong way to go for this
without more information on the reason why it is difficult to say what
the correct approach would be

--
Calm down, it's *____    only* ones and zeroes.


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Wojtek  
View profile  
 More options Sep 28 2012, 10:51 am
Newsgroups: comp.lang.python
From: Wojtek <de...@nospam.poczta.fm>
Date: Fri, 28 Sep 2012 16:51:07 +0200
Local: Fri, Sep 28 2012 10:51 am
Subject: Re: howto handle nested for
W dniu 2012-09-28 16:42, Alister pisze:

it's well described in head first: python book ;)
check this sources from this book
http://www.headfirstlabs.com/books/hfpython/code/chapter1.zip

hope it helps,

regards


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Laszlo Nagy  
View profile  
 More options Sep 28 2012, 11:04 am
Newsgroups: comp.lang.python
From: Laszlo Nagy <gand...@shopzeus.com>
Date: Fri, 28 Sep 2012 17:04:38 +0200
Local: Fri, Sep 28 2012 11:04 am
Subject: Re: howto handle nested for
On 2012-09-28 16:39, Neal Becker wrote:

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]


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Tim Chase  
View profile  
 More options Sep 28 2012, 11:27 am
Newsgroups: comp.lang.python
From: Tim Chase <python.l...@tim.thechases.com>
Date: Fri, 28 Sep 2012 10:28:21 -0500
Local: Fri, Sep 28 2012 11:28 am
Subject: Re: howto handle nested for
On 09/28/12 09:39, Neal Becker wrote:

There was a good discussion on this back in 2008 that might be worth
reading over.  For some reason the mail.python.org archives[1] seem
to have broken threading on this topic (Andrew Reedick's reply using
exec() is waaay down in the archive, disassociated from the thread),
so here it is archived somewhere else where the 2 pages of threading
seems more manageable/accurate:

http://www.velocityreviews.com/forums/t585147-creating-unique-combina...

-tkc

[1]
http://mail.python.org/pipermail/python-list/2008-January/487851.html


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Ian Kelly  
View profile  
 More options Sep 28 2012, 11:50 am
Newsgroups: comp.lang.python
From: Ian Kelly <ian.g.ke...@gmail.com>
Date: Fri, 28 Sep 2012 09:49:36 -0600
Local: Fri, Sep 28 2012 11:49 am
Subject: Re: howto handle nested for

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

Cheers,
Ian


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Neal Becker  
View profile  
 More options Sep 28 2012, 11:53 am
Newsgroups: comp.lang.python
From: Neal Becker <ndbeck...@gmail.com>
Date: Fri, 28 Sep 2012 11:52:36 -0400
Local: Fri, Sep 28 2012 11:52 am
Subject: Re: howto handle nested for

Thanks for the suggestions: I found itertools.product is just great for this.

 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Peter Otten  
View profile  
 More options Sep 28 2012, 11:54 am
Newsgroups: comp.lang.python
From: Peter Otten <__pete...@web.de>
Date: Fri, 28 Sep 2012 17:53:54 +0200
Local: Fri, Sep 28 2012 11:53 am
Subject: Re: howto handle nested for

for s in itertools.product(range(n_syms), repeat=6):
    print s

 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Neil Cerutti  
View profile  
 More options Sep 28 2012, 4:38 pm
Newsgroups: comp.lang.python
From: Neil Cerutti <ne...@norwich.edu>
Date: 28 Sep 2012 20:38:56 GMT
Local: Fri, Sep 28 2012 4:38 pm
Subject: Re: howto handle nested for
On 2012-09-28, Laszlo Nagy <gand...@shopzeus.com> wrote:

It looks like you might have missed the last one. Also, be sure
to check itertools for occasionally for cool stuff like this.

>>> for values in itertools.product(range(3), repeat=2):

...   print(values)
...
(0, 0)
(0, 1)
(0, 2)
(1, 0)
(1, 1)
(1, 2)
(2, 0)
(2, 1)
(2, 2)

--
Neil Cerutti


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Peter Pearson  
View profile  
 More options Sep 28 2012, 9:15 pm
Newsgroups: comp.lang.python
From: Peter Pearson <ppear...@nowhere.invalid>
Date: 29 Sep 2012 01:15:24 GMT
Local: Fri, Sep 28 2012 9:15 pm
Subject: Re: howto handle nested for

On Fri, 28 Sep 2012 09:49:36 -0600, Ian Kelly <ian.g.ke...@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.


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Hans Mulder  
View profile  
 More options Sep 29 2012, 8:41 am
Newsgroups: comp.lang.python
From: Hans Mulder <han...@xs4all.nl>
Date: Sat, 29 Sep 2012 14:41:40 +0200
Local: Sat, Sep 29 2012 8:41 am
Subject: Re: howto handle nested for
On 29/09/12 03:15:24, Peter Pearson wrote:

...     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


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
End of messages
« Back to Discussions « Newer topic     Older topic »