1.numpy 返回index
import numpy
a = numpy.array(([3,2,1],[2,5,7],[4,7,8]))
itemindex = numpy.argwhere(a == 7)
print (itemindex)
print a
[[1 2]
[2 1]]
[[3 2 1]
[2 5 7]
[4 7 8]]
2.list返回index
index() 方法检测字符串中是否包含子字符串 str ,如果指定 beg(开始) 和 end(结束) 范围,则检查是否包含在指定范围内,该方法与 python find()方法一样,只不过如果str不在 string中会报一个异常。影响后面程序执行。只返回元素的第一个值的index.
index()方法语法:str.index(str, beg=0, end=len(string))
str -- 指定检索的字符串
beg -- 开始索引,默认为0。
end -- 结束索引,默认为字符串的长度。
str1=[2,3,4,5]
str2=2
#空格,等其他操作符对其索引位置也有影响
#在str1中检测字符串中是否含有子字符串str2 str1.index(str2,beg=0,end=len(str1))
#如果包含子字符串 返回检测到的索引值
print(str1.index(str2))
#从索引0开始检测,检测长度为3
print(str1.index(str2,0,3))
0
0
import numpy
a = numpy.array(([[3,2,1],[2,5,7],[4,7,8]]))
b = [12,3,4,5,6,7,7,7,8,7,8,9,8,7]
itemindex = numpy.argwhere(a == 7)
indexlist = b.index(7)
print (itemindex)
print(indexlist)
print (a)
[[1 2]
[2 1]]
5
[[3 2 1]
[2 5 7]
[4 7 8]]
如上所示,list index()方法只返回遇到的第一个元素的下标值,numpy的argwhere(a==x)可返回numpy内任意元素的索引位置(包括维数,所有符合条件的都输出)