4

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.

1
  • 2
    Why did you imagine "wildcarding" would work? Just a guess??? Also, are you working with an array or a list, as your output suggests? Commented Feb 10, 2018 at 0:56

2 Answers 2

6

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]
Sign up to request clarification or add additional context in comments.

Comments

4

You can use numpy for this:

import numpy as np

arr = [(0.109, 0.5), (0.109, 0.55), (0.109, 0.6)]
arr = np.array(arr)

arr[:, 1]  # array([ 0.5 ,  0.55,  0.6 ])
arr[0, :]  # array([ 0.109,  0.5  ])

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.