首页 > 技术文章 > 获得一个list中某元素的索引值

cymwill 2017-08-30 18:05 原文

list = [1,2,3,3,2,1]
list.index(1) # 只能获得首个1的索引值

如果要获得所有该元素的索引值

import numpy as np
arr = np.array(list)
index = np.where(arr==1)
index[0]

然后才能用

for i in index[0]:
    print(i)

 

推荐阅读