python - 如何获取非整数元素的索引

标签 python arrays numpy

在下面的numpy数组(数据)中,如何获取非整数的索引位置,例如4.5和6.7?

import numpy as np
data = np.array([2.0, 3.0, 4.5, 6.7, 12.0],dtype=float)
print data

由于我正在处理非常大的数组,因此要考虑更快的处理速度。

最佳答案

为了提高速度,您应该使用 np.where。现在查找元素是否为整数的一种解决方案是将其与其舍入值进行比较:

np.where(data != data.round())
(array([2, 3]),)

另一个解决方案是使用nonzero:

(data - data.round()).nonzero()
(array([2, 3]),)

关于python - 如何获取非整数元素的索引,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23308319/

相关文章:

Python - 不确定如何将列中的行值汇总到列表中

python - Numpy 和 TensorFlow 之间的差异

arrays - Swift:CoreData 和一个带有数组的对象

c - 处理C中的字符串输入

c# - C# 中数组的幕后

python - 使用 numpy 提高组合 for block 的性能

python - 如何使用numpy创建n维向量的均匀网格(换句话说,均匀填充一个n维超立方体)?

python - Pygame 显示更新故障?

multithreading - 移动窗口时 PyQt4 视频播放器崩溃

python - 如何在给定基础的情况下恢复类的 mro?