Array Algorithm

This is the place for queries that don't fit in any of the other categories.

Array Algorithm

Postby mikeh » Wed Jan 08, 2014 7:26 am

I just started learning python and have run into a problem in my current project. I am trying to loop through an array to check every possible combination of the array within a certain range. The problem is that the length of the array is going to change based on a user input so I can't just write a loop for every index in the array. Does anyone have any tips for how to write this?

For example, if it had a length of 2 and I wanted it to count from 0 to 3 I would want it to loop through and print out:
[0, 0] [0, 1] [0, 2] [0, 3] [1, 0] [1, 1] [1, 2] [1, 3] [2, 0] [2, 1] [2, 2] [2, 3] [3, 0] [3, 1] [3, 2] [3, 3]
I don't need it to print them all out on a single line but I just need for the loop or module to give me all of the outputs at some point.
And I could even have it go out to length 10 or something, idk yet. I have most of the code for the rest of the program done too, just need this one thing.
Last edited by micseydel on Wed Jan 08, 2014 9:05 am, edited 1 time in total.
Reason: Locked OP.
mikeh
 
Posts: 3
Joined: Wed Jan 08, 2014 7:16 am

Re: Array Algorithm

Postby micseydel » Wed Jan 08, 2014 9:06 am

Check out this post. We'd like to see your code (in code tags) and an explanation of why it doesn't do what you want. More details about how to make a good post are in the aforementioned link.
Join the #python-forum IRC channel on irc.freenode.net!
User avatar
micseydel
 
Posts: 1179
Joined: Tue Feb 12, 2013 2:18 am
Location: Mountain View, CA

Re: Array Algorithm

Postby Mekire » Wed Jan 08, 2014 10:06 am

You will most certainly want to use itertools for this.
Post your current attempt and we can work from there.

-Mek
User avatar
Mekire
 
Posts: 986
Joined: Thu Feb 07, 2013 11:33 pm
Location: Amakusa, Japan

Re: Array Algorithm

Postby mikeh » Wed Jan 08, 2014 8:05 pm

It looks like the itertools would definitely work for it, I think the one that looked most similar to what I'm looking for was combinations_with_replacement.

Code: Select all
import itertools

array= []
   
for i in range(9):   
    print itertools.combinations_with_replacement(range(3), 3)


This is what I was testing to see if it would return the correct lists when i looped through it. I also tried appending all of the outputs into a blank array but that also returned the same results where it called <itertools.combinations_with_replacement object at 0x7f1da0b15100> at every spot.
mikeh
 
Posts: 3
Joined: Wed Jan 08, 2014 7:16 am

Re: Array Algorithm

Postby ochichinyezaboombwa » Thu Jan 09, 2014 7:08 am

The thing is, combinations_with_replacement (as many other functions from itertools) returns a generator rather than a list. A generator is an object you can iterate through (using it's next() method or list comprehension), but it doesn't, unlike list, contain all elements of the iteration at once / at start. This might be confusing but is very useful in practice as it is much more memory-efficient.

Anyway, you use a generator one element at a time. Using your example:
Code: Select all
>>> x = itertools.combinations_with_replacement(range(3), 3)
>>> x.next()
(0, 0, 0)
>>> x.next()
(0, 0, 1)
>>> x.next()
(0, 0, 2)
etc.

Or probably much better in your case: make a list out of the whole generator at once, like this, and then print it:
Code: Select all
>>> print [x for x in itertools.combinations_with_replacement(range(3), 3)]
[(0, 0, 0), (0, 0, 1), (0, 0, 2), (0, 1, 1), (0, 1, 2), (0, 2, 2), (1, 1, 1), (1, 1, 2), (1, 2, 2), (2, 2, 2)]
Or, simpler:
Code: Select all
print list(itertools.combinations_with_replacement(range(3), 3))
[(0, 0, 0), (0, 0, 1), (0, 0, 2), (0, 1, 1), (0, 1, 2), (0, 2, 2), (1, 1, 1), (1, 1, 2), (1, 2, 2), (2, 2, 2)]

To me, a question remains why you use the loop
Code: Select all
for i in range(9):
-- as you never use the i.
ochichinyezaboombwa
 
Posts: 200
Joined: Tue Jun 04, 2013 7:53 pm

Re: Array Algorithm

Postby mikeh » Sun Jan 12, 2014 10:15 pm

I just needed it to loop through and then do a few actions on the tuple every time i called for the next one.
It is working now but I realized that it is not giving me the desired tuples still. When I looked through the itertools available I could not find one that gave me what was desired still. Permutations is closer, but still does not work while permutations gives
Code: Select all
# permutations(range(3)) --> 012 021 102 120 201 210
I am looking for something where I could get these outputs from the given inputs
Code: Select all
# function(range(3), 3) --> 000 001 002 010 011 012 020 021 022 100 101 102 110 111 112 120 121 122 200 201 202 210 211 212 220 221 222
mikeh
 
Posts: 3
Joined: Wed Jan 08, 2014 7:16 am

Re: Array Algorithm

Postby stranac » Sun Jan 12, 2014 10:21 pm

You're looking for itertools.product()
Code: Select all
>>> import itertools
>>> for x in itertools.product(range(3), repeat=3):
...     print x
...
(0, 0, 0)
(0, 0, 1)
(0, 0, 2)
(0, 1, 0)
(0, 1, 1)
(0, 1, 2)
(0, 2, 0)
(0, 2, 1)
(0, 2, 2)
(1, 0, 0)
(1, 0, 1)
(1, 0, 2)
(1, 1, 0)
(1, 1, 1)
(1, 1, 2)
(1, 2, 0)
(1, 2, 1)
(1, 2, 2)
(2, 0, 0)
(2, 0, 1)
(2, 0, 2)
(2, 1, 0)
(2, 1, 1)
(2, 1, 2)
(2, 2, 0)
(2, 2, 1)
(2, 2, 2)
Friendship is magic!

R.I.P. Tracy M. You will be missed.
User avatar
stranac
 
Posts: 1093
Joined: Thu Feb 07, 2013 3:42 pm


Return to General Coding Help

Who is online

Users browsing this forum: No registered users and 4 guests