How to iterate over this n-dimensional dataset?

I have datasetone that has 4 dimensions (for now ...), and I need to iterate over it.

To access the value in dataset, I do this:

value = dataset[i,j,k,l]

Now I can get shapefor dataset:

shape = [4,5,2,6]

The values ​​in shaperepresent the length of the measurement.

How, given the number of dimensions, is it possible to iterate over all the elements in my dataset? Here is an example:

for i in range(shape[0]):
    for j in range(shape[1]):
        for k in range(shape[2]):
            for l in range(shape[3]):
                print('BOOM')
                value = dataset[i,j,k,l]

The future shapemay change. So, for example, it shapemay contain 10 elements, not the current 4.

Is there a nice and clean way to do this using Python 3?

+6
source share
1 answer

itertools.product cartesian 1 ( ):

import itertools
shape = [4,5,2,6]
for idx in itertools.product(*[range(s) for s in shape]):
    value = dataset[idx]
    print(idx, value)
    # i would be "idx[0]", j "idx[1]" and so on...

, numpy, , np.ndenumerate:

import numpy as np

arr = np.random.random([4,5,2,6])
for idx, value in np.ndenumerate(arr):
    print(idx, value)
    # i would be "idx[0]", j "idx[1]" and so on...

1 , itertools.product(*[range(s) for s in shape]). .

, :

for i in range(10):
    for j in range(8):
        # do whatever

product :

for i, j in itertools.product(range(10), range(8)):
#                                        ^^^^^^^^---- the inner for loop
#                             ^^^^^^^^^-------------- the outer for loop
    # do whatever

, product - .

for -loops product, :

# Create the "values" each for-loop iterates over
loopover = [range(s) for s in shape]

# Unpack the list using "*" operator because "product" needs them as 
# different positional arguments:
prod = itertools.product(*loopover)

for idx in prod:
     i_0, i_1, ..., i_n = idx   # index is a tuple that can be unpacked if you know the number of values.
                                # The "..." has to be replaced with the variables in real code!
     # do whatever

:

for i_1 in range(shape[0]):
    for i_2 in range(shape[1]):
        ... # more loops
            for i_n in range(shape[n]):  # n is the length of the "shape" object
                # do whatever
+6

All Articles