A question about making a sort-of-counter.
crebert at ucsd.edu
Wed Mar 31 00:08:17 CEST 2010
On Tue, Mar 30, 2010 at 2:31 PM, Justin Park <hp6 at rice.edu> wrote:
> Suppose I have a list.
> a = list()
> And suppose allowed digits as the element are 1,2,3,4,5.
> What can I do in order to iterate over all possible values for each element?
> For instance, the sequence of the list I want to have would be
> How can I make it happen?
allowed = range(1,6)
length = 5
for counter_tuple in product(allowed, repeat=length):
counter_list = list(counter_tuple) # if you really need a list
#do whatever with the counter value
See the docs for itertools.product(); an example virtually identical
to your situation is given:
More information about the Python-list