'View ndarray as list of indexes and list of values

I have an ND-array with 2 or 6 dimensions. For example:

nd_array = [[90, 80],
            [70, 60], 
            [50, 40]]

and I want to view it as a list of indices like so:

nd_array_transformed = [[90, 0, 0],
                        [80, 0, 1],
                        [70, 1, 0],
                        [60, 1, 1],
                        [50, 2, 0], 
                        [40, 2, 1]]

or a similar representation.

I found this code for the 2D case, but I'm looking for a more general solution:

np.array([[nd_array[row][col], row, col] for row in range(nd_array.shape[0]) 
                                         for col in range(nd_array.shape[1])])

Is it possible to make this transformation in the general case, perhaps using *nd_array.shape?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source