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?
On Fri, 28 Sep 2012 10:39:32 -0400, 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?
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
>> 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?
> 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
> 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?
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
> 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?
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:
On Fri, Sep 28, 2012 at 8:39 AM, Neal Becker <ndbeck...@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
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?
Thanks for the suggestions: I found itertools.product is just great for this.
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?
for s in itertools.product(range(n_syms), repeat=6):
print s
> 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: