Python 获取numpy.array索引值的实例
作者:John9ML 时间:2021-01-19 21:36:57
举个例子:
q=[0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15]
我想获取其中值等于7的那个值的下标,以便于用于其他计算。
如果使用np.where,如:
q=np.arange(0,16,1)
g=np.where(q==7)
print q
print g
运行结果是:
[ 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15]
(array([7]),)
显然(array([7]),)中的数字7我是没法提取出来做运算的,这是一个tuple。
处理方法是:
q=np.arange(0,16,1)
g=np.argwhere(q==7)//注意这里,返回的是下标而不是索引值
print q
print g
print int(g)//转化为可运算的数字
运行结果为:
[ 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15]
[[7]]
7
来源:https://blog.csdn.net/weixin_38570251/article/details/82120442
标签:Python,numpy,array,索引值
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
WEB2.0网页制作标准教程(6)XHTML代码规范
2007-12-13 13:03:00
python常见字符串处理函数与用法汇总
2023-10-19 08:07:11
《色彩解答》系列之三 色彩对比
2008-02-17 14:40:00
![](https://img.aspxhome.com/file/UploadPic/20082/17/2008217144426798s.png)
Python seaborn barplot画图案例
2023-11-07 00:50:55
![](https://img.aspxhome.com/file/2023/7/63977_0s.png)
解决python 输出是省略号的问题
2021-01-09 13:38:49
Python常用库Numpy进行矩阵运算详解
2023-01-04 02:45:52
Python格式化文本段落之textwrap库
2023-11-23 00:40:56
![](https://img.aspxhome.com/file/2023/6/92106_0s.png)
Python实现的自定义多线程多进程类示例
2023-11-16 08:52:15
Python-jenkins模块获取jobs的执行状态操作
2022-07-29 14:15:09
![](https://img.aspxhome.com/file/2023/9/70809_0s.jpg)
解剖JavaScript中的null和undefined
2009-03-01 12:49:00
Python使用win32com.client的方法示例
2021-03-22 14:32:48
Python基础 while循环与break、continue关键字
2023-11-19 03:41:42
![](https://img.aspxhome.com/file/2023/3/64283_0s.jpg)
使用PyCharm官方中文语言包汉化PyCharm
2023-03-20 23:42:38
![](https://img.aspxhome.com/file/2023/7/100807_0s.png)
python实战之德州扑克第三步-比较大小
2022-01-01 22:27:11
python实现记事本功能
2022-04-02 09:54:19
![](https://img.aspxhome.com/file/2023/1/75221_0s.gif)
Python中断言Assertion的一些改进方案
2023-03-05 15:07:57
Python实现读取机器硬件信息的方法示例
2021-02-04 16:28:55
Python实现PS滤镜中的USM锐化效果
2023-07-10 12:58:24
![](https://img.aspxhome.com/file/2023/0/95230_0s.png)
python 元组和列表的区别
2021-08-25 19:01:27
![](https://img.aspxhome.com/file/2023/3/65493_0s.png)
sqlserver 数据库压缩与数据库日志(ldf)压缩方法分享
2012-01-05 18:57:19