xxxxxxxxxx
In [12]: a = np.array([[1,2,3], [4,5,6]])
In [13]: b = a.ravel()
In [14]: b
Out[14]: array([1, 2, 3, 4, 5, 6])
Here is one way you could do this:
xxxxxxxxxx
import numpy as np
# Suppose you have a two-dimensional array
arr = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
# You can use np.meshgrid to generate the x and y indices
x_idx, y_idx = np.meshgrid(range(arr.shape[0]), range(arr.shape[1]))
# You can then flatten these indices and the values to get a single array
result = np.column_stack((x_idx.flatten(), y_idx.flatten(), arr.flatten()))
# This will give you the following array
np.array([[0, 0, 1],
[1, 0, 2],
[2, 0, 3],
[0, 1, 4],
[1, 1, 5],
[2, 1, 6],
[0, 2, 7],
[1, 2, 8],
[2, 2, 9]])
xxxxxxxxxx
# Python convert 2D into 1D array:
import itertools
x = [['foo'], ['bar', 'baz'], ['quux'], ("tup_1", "tup_2"), {1:"one", 2:"two"}]
print list(itertools.chain(*x))
print [element for sub in x for element in sub]
# Output:
['foo', 'bar', 'baz', 'quux', 'tup_1', 'tup_2', 1, 2]