Numpy.where函数找不到数组中的值...任何人都知道为什么?

Law*_*nce 8 python arrays numpy where

我一直在尝试使用python的numpy.where函数来确定特定值的位置,但由于某种原因,它错误地确定False了实际找到值的位置.从而返回一个空数组.见下文:

>>>lbpoly=numpy.array([ 5.45  5.5   5.55  5.6   5.65  5.7   5.75  5.8   5.85  5.9   5.95  6.
6.05  6.1   6.15  6.2   6.25  6.3   6.35  6.4   6.45  6.5   6.55  6.6
6.65  6.7   6.75  6.8   6.85  6.9   6.95  7.  ])

>>>cpah=numpy.where(lbpoly==6.2)

>>>print cpah

>>>(array([], dtype=int32),)
Run Code Online (Sandbox Code Playgroud)

有谁知道为什么会这样?我尝试了许多不同的变体,甚至使用<>逻辑.但这会产生2个值的指数.

Ami*_*ory 7

比较浮点数没有什么意义; 特别是,通过观察浮点数的打印表示,您无法学到很多东西.(在这里查看更多.)

尝试这样的事情:

import numpy as np

lbpoly= np.array([4.0, 6.2])

>>> np.where(np.isclose(lbpoly, 6.2))
(array([1]),)
Run Code Online (Sandbox Code Playgroud)