I have two parts question for this
X = [[[-1.0, -1.0], [-2.0, -1.9], [-3.4, -2.0], [3.0, 1.5], [3.7, 1.0]],
[[3.0, 2.0] , [-4.0, 10.0]],
[[-10.0, 5.0], [-6.0, -10.0]],
[[2.0, -2.0]]]
first I wnat to iterate over every single column of the chunk ? How to do it?
this is my approach :
ds = zip(*X)
for list in ds:
print(list)
but it's giving me only one column: ([-1.0, -1.0], [3.0, 2.0], [-10.0, 5.0], [2.0, -2.0])
second how to creat 2 points for each column?
CodePudding user response:
Unclear what your expected output is but:
data = [
[[-1.0, -1.0], [-2.0, -1.9], [-3.4, -2.0], [3.0, 1.5], [3.7, 1.0]],
[[3.0, 2.0], [-4.0, 10.0]],
[[-10.0, 5.0], [-6.0, -10.0]],
[[2.0, -2.0]]
]
flattened = [column for row in data for column in row]
Output:
[[-1.0, -1.0],
[-2.0, -1.9],
[-3.4, -2.0],
[3.0, 1.5],
[3.7, 1.0],
[3.0, 2.0],
[-4.0, 10.0],
[-10.0, 5.0],
[-6.0, -10.0],
[2.0, -2.0]]
CodePudding user response:
Use itertools.zip_longest
:
from itertools import zip_longest
for lst in zip_longest(*X):
print(lst)
Output:
([-1.0, -1.0], [3.0, 2.0], [-10.0, 5.0], [2.0, -2.0])
([-2.0, -1.9], [-4.0, 10.0], [-6.0, -10.0], None)
([-3.4, -2.0], None, None, None)
([3.0, 1.5], None, None, None)
([3.7, 1.0], None, None, None)
If you don't want None
s:
for lst in zip_longest(*X):
print(lst[:(lst.index(None) if None in lst else None)])
([-1.0, -1.0], [3.0, 2.0], [-10.0, 5.0], [2.0, -2.0])
([-2.0, -1.9], [-4.0, 10.0], [-6.0, -10.0])
([-3.4, -2.0],)
([3.0, 1.5],)
([3.7, 1.0],)
CodePudding user response:
Not sure what you mean by 'every column'. When I look at your nested list X
, it seems to be a list of 2D nested lists each with 2 columns and a variable number of rows:
>>> import numpy as np
>>> [np.array(ch).shape for ch in X]
[(5, 2), (2, 2), (2, 2), (1, 2)]
In your question you describe the following as a column:
([-1.0, -1.0], [3.0, 2.0], [-10.0, 5.0], [2.0, -2.0])
This is the first row of each of the 2D arrays I describe above. Since the last of these only has one row, the zip operation will stop after the first row because it runs out of items.
Maybe you are trying to concatenate all the 2D arrays into a vertical stack.
>>> np.vstack(X)
array([[ -1. , -1. ],
[ -2. , -1.9],
[ -3.4, -2. ],
[ 3. , 1.5],
[ 3.7, 1. ],
[ 3. , 2. ],
[ -4. , 10. ],
[-10. , 5. ],
[ -6. , -10. ],
[ 2. , -2. ]])
Or, if you don't want to use numpy, as @Lukas suggested:
>>> [row for chunk in X for row in chunk]
[[-1.0, -1.0],
[-2.0, -1.9],
[-3.4, -2.0],
[3.0, 1.5],
[3.7, 1.0],
[3.0, 2.0],
[-4.0, 10.0],
[-10.0, 5.0],
[-6.0, -10.0],
[2.0, -2.0]]
Also, note that you should not use the Python keyword list
as a variable.