PEP 8:与 True 的比较应该是 'if cond is True:' 或 'if cond:'

Pri*_*nce 3 python pycharm

当我执行 np.where(temp == True) 时,PyCharm 会发出警告

我的完整代码:

from numpy import where, array

a = array([[0.4682], [0.5318]])
b = array([[0.29828851, 0., 0.28676873, 0., 0., 0., 0., 0.28801431, 0., 0., 0.71283046, 0.],
          [0.70171149, 0., 0.71323127, 0., 0., 0., 0., 0.71198569, 0., 0., 0.28716954, 0.]])

temp = b > 1.1*a
pos = where(temp == True)

print(pos) 
Run Code Online (Sandbox Code Playgroud)

如果我按照其他帖子中的建议将 temp == True 更改为 temp 为 True,则代码无法按预期工作。

这个警告应该如何解决?

where(temp) 工作。非常感谢 !!@Joao Vitorino 感谢您的解释,@jedwards。它有助于。

Joa*_*ino 5

不要将布尔值与布尔值进行比较。

你应该检查是真还是假。

b == true
if b: # If b is True
   do something 
Run Code Online (Sandbox Code Playgroud)

在你的情况下

temp = b > 1.1*a
pos = where(temp)  
Run Code Online (Sandbox Code Playgroud)

这里有一些解释