Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python get column vector from array of tuples

Tags:

python

arrays

I have an array of tuples (from prev processing of a structured array, but the filed info was lost).

>>> arr
[(0.109, 0.5), (0.109, 0.55), (0.109, 0.6)]
>>> 

I need to extract the column vectors for first and second column.

Using two indices with fixed values works OK, but wildcarding the row fails.

>>> arr[0][1]
0.5
>>> arr[*][1]
  File "<stdin>", line 1
    arr[*][1]
                      ^
SyntaxError: invalid syntax
>>> 

Your feedback is appreciated.

like image 418
Gert Gottschalk Avatar asked Sep 11 '25 20:09

Gert Gottschalk


1 Answers

To get a list that contains the first element of each tuple:

[elem[0] for elem in arr]

...and the second element:

[elem[1] for elem in arr]
like image 120
Ronan Boiteau Avatar answered Sep 13 '25 10:09

Ronan Boiteau