jpcgandre
jpcgandre

Reputation: 1505

find index of row matching a given criteria

I have the following list (nodes):

nodeID, x, y, z=row

I want to find the index of the row which row[0]==nodeAID.

My code is:

nindF=[line[0].index(nodeAID) for line in nodes]

but it gives me the error: TypeError: expected a character buffer object

Upvotes: 0

Views: 243

Answers (1)

Ionut Hulub
Ionut Hulub

Reputation: 6326

nindF = [index for index, line in enumerate(nodes) if line[0].find(nodeAID) >= 0]

This will return a list of indexes of all the lines that start with nodeAID. If you only care about the index of the first line that starts with nodeAID then:

nindF = [index for index, line in enumerate(nodes) if line[0].find(nodeAID) >= 0][0]

Upvotes: 1

Related Questions