简体   繁体   English

查找与给定条件匹配的行的索引

[英]find index of row matching a given criteria

I have the following list ( nodes ): 我有以下列表( nodes ):

nodeID, x, y, z=row

I want to find the index of the row which row[0]==nodeAID . 我想找到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 但它给了我错误: TypeError: expected a character buffer object

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. 这将返回以nodeAID开头的所有行的索引列表。 If you only care about the index of the first line that starts with nodeAID then: 如果您只关心以nodeAID开头的第一行的索引,则:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM