简体   繁体   中英

find position of element in matrix

I have a list of lists. I want to find the position of the string "1" in one particular column col:

from numpy import transpose
col = 0
lists = [["0", "0", "N"], ["1", "0", "N"], ["N", "N", "N"]]
pos = transpose(lists)[col].index("1")

But I get the error:

AttributeError: 'numpy.ndarray' object has no attribute 'index'

Somehow importing transpose from numpy prevents me to use index(). How do we do this easily ?

Using np.transpose converts your native list to np.array which is why you get the error.

Using numpy:

np.where(np.array(lists)[:,0]=="1")

Using native lists:

list(zip(*lists))[0].index("1")

list(zip(*lists)) is a native way of transposing...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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