numpy array to list formatting
arrays, list, numpy, python
Solution
Use list comprehension and slicing:
>>> data1 = np.array([[0,0,0],[0,1,1],[1,0,1],[1,1,0]])
>>> print [[x[:2].tolist(), x[2:].tolist()] for x in data1]
[[[0, 0], [0]],
[[0, 1], [1]],
[[1, 0], [1]],
[[1, 1], [0]]]
Problem
How would i format a numpy array of form ``` data1 = np.array([[0,0,0],[0,1,1],[1,0,1],[1,1,0]]) ``` to a list in this format: ``` data = [ [[0,0], [0]], [[0,1], [1]], [[1,0], [1]], [[1,1], [0]] ] ``` I tried using two for loops ``` for i in range(len(data)): for j in range(3): if j == 2: va[i] = data1[i][j] else: sa[i] = data1[i][j] ``` but that gives me an index out of bounds error. I would love some ideas on how to go about this