howto handle nested for

Hans Mulder hansmu at xs4all.nl
Sat Sep 29 08:41:40 EDT 2012


On 29/09/12 03:15:24, Peter Pearson wrote:
> On Fri, 28 Sep 2012 09:49:36 -0600, Ian Kelly <ian.g.kelly at 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






More information about the Python-list mailing list