PYTHON
convert 2d list to 1d python
import itertools
a = [[1, 2], [3, 4], [5, 6]]
list(itertools.chain.from_iterable(a))
Output:- [1, 2, 3, 4, 5, 6]
numpy convert 1d to 2d
a = np.arange(6).reshape((3, 2))
>>> a
array([[0, 1],
[2, 3],
[4, 5]])
convert 2d array to 1d c++
grid1D[row*col];
grid2D[row][col];
for(int i = 0; i < row; ++i)
for(int j = 0; j < col; ++j)
grid1D[i * col + j] = grid2D[i][j];
convert 2d aray into 1d using python
import numpy as np
twoDAry = [6.4],[5.9],[5.5],[5.3],[5.1],[4.9],[4.5],[4.5],[4.5],[4.3],[4.2],[3.8],[3.5],[2.8],[2.8],[2.8]
twoDAry = np.array(twoDAry)
print(twoDAry.reshape(1, -1)[0])
# Output
# [6.4 5.9 5.5 5.3 5.1 4.9 4.5 4.5 4.5 4.3 4.2 3.8 3.5 2.8 2.8 2.8]
python 1d array into 2d array
arr2d = [[arr1d[i+j*width] for i in range(width)] for j in range(height)]
pass 2d array to 1d python
# Create a 2D Numpy Array.
arr = np. array([[0, 1, 2],
[3, 4, 5],
[6, 7, 8]])
# convert 2D array to a 1D array of size 9.
flat_arr = np. reshape(arr, 9)
convert 2d array to 1d
# Python code to demonstrate
# flattening a 2d numpy array
# into 1d array
import numpy as np
ini_array1 = np.array([[1, 2, 3], [2, 4, 5], [1, 2, 3]])
# printing initial arrays
print("initial array", str(ini_array1))
# Multiplying arrays
result = ini_array1.flatten()
# printing result
print("New resulting array: ", result)
array 2d to 1d
int array[width * height];
int SetElement(int row, int col, int value)
{
array[width * row + col] = value;
}