I read in the following object with numpy.genfromtxt:
A = [(4, 'A', 3750.5),
(4, 'B', 3252.6),
(8, 'A', 3350.5),
(8, 'B', 3152.6)]
I would like to do numpy fancy indexing on it, but I can't because this is not an numpy array. It's an array of a list.
What would be the best way to get the 3rd column of all rows that have '4' in the first column?
I tried A[A[:,0]==4] but the interpreter complained with "IndexError: invalid index".
Edit:
This is the python program I am using:
import numpy as np
A = np.genfromtxt( "text.txt" , dtype=( int , "|S10", float))
A_array = np.asarray(A, dtype=object)
print A
print A_array
The file text.txt:
4 A 3750.5
4 B 3270.5
8 A 3480.5
8 B 3590.5
This is the output:
[(4, 'A', 3750.5) (4, 'B', 3270.5) (8, 'A', 3480.5) (8, 'B', 3590.5)]
[(4, 'A', 3750.5) (4, 'B', 3270.5) (8, 'A', 3480.5) (8, 'B', 3590.5)]
What am I missing here?
Ais actually a list of tuples, not an array of lists.A = ...implying that you're assigning the data. When you "print" data, you don't sayA = ..., you just print the contents. When you assign to a variable withA = ...you need the value on the right-hand-side of the=symbol to evaluate to a valid Python object, which is not true without the commas.