A question about making a sort-of-counter.

J

Justin Park

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
[1,1,1,1,1]
[1,1,1,1,2]
[1,1,1,1,3]

.....

[5,5,5,5,4]
[5,5,5,5,5]

How can I make it happen?

Thanks,
Justin.
 
A

anon

Justin said:
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
[1,1,1,1,1]
[1,1,1,1,2]
[1,1,1,1,3]

....

[5,5,5,5,4]
[5,5,5,5,5]

How can I make it happen?

Thanks,
Justin.


http://docs.python.org/library/itertools.html#itertools.product

from itertools import product

for item in product([1, 2], repeat=2):
print item
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,175
Messages
2,570,944
Members
47,491
Latest member
mohitk

Latest Threads

Top