numpy,list返回index

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内任意元素的索引位置(包括维数,所有符合条件的都输出)

  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

智能学习者

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值