0

I have a numpy array with shape (n,), for example:

[
    [0, 1],         # A
    [0, 1, 2, 3],   # B
    [0, 1, 2]       # C
]

How can I iterate through all 'tuples' formed by this array, in this case I would expect the return to be (0,0,0),(1,0,0),(0,1,0),(0,2,0),(0,3,0),(1,1,0)... and so on

5
  • how is 0,1,0 formed by this array? Commented Sep 10, 2020 at 14:08
  • zeroth element from array A, first element from array B and zeroth element from array C Commented Sep 10, 2020 at 14:10
  • 2
    Something like for tup in itertools.product(*your_array): The ordering won't be as shown above, but to be honest what you've shown there seems a somewhat random order anyway. Commented Sep 10, 2020 at 14:14
  • 1
    That isn't a numpy array, that is a list with other lists inside of it. Or are you saying you have a numpy array with dtype = object that contains lists? Of so, why? Commented Sep 10, 2020 at 15:02
  • @juanpa.arrivillaga, I guess you are right. He created a list of lists with different lengths, and convert to numpy array. Commented Sep 10, 2020 at 16:33

1 Answer 1

1

Here is a numpy solution for you.

inp = [
    [0, 1,],        
    [0, 1, 2, 3],  
    [0, 1, 2]       
]

Now you can simply

import numpy as np

shp = []
for sub_list in inp:
    shp.append(len(sub_list))

arr = np.ones(shp)

result = np.where(arr)

tuples = [t for t in zip(*result)]

out:

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

The way this works, is you build an array of ones whose dimensions are the lengths of your lists.
Then you get the multi dimensional indices of this array, which happen to be exactly what you wanted.

If you also want to access your lists in the relevant index, you can easily do that as well.

Sign up to request clarification or add additional context in comments.

5 Comments

Don't reuse the built-in input as a variable. What does shp mean? Pretty sure result = np.where(np.ones(shp)) is just converting the shp list to a numpy array.
@Guimoute Edited input into inp. thanks. shp is the shape of an array whose dims are the lengths of the lists in inp. This solution is not efficient at all, but it uses numpy as per the OP's request.
@Guimoute the np.where returns a tuple of lists, each list holds the index in the relevant dimension.
Let's say that the values used by the OP are just the first example he could think of… what happens if we use. e.g., inp = [[3,2], [1,5,2,1], [8,6]]?
@gboffi then this solution is no better than the one posted as a comment to the question by alani and is probably worse. You would have to iterate using these generated indices, and access the list.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.