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

2024-02-24

当我执行 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) 

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

这个警告应该如何解决?

(温度)在哪里工作。多谢 !! @若奥·维托里诺 感谢您的解释,@jedwards。它有助于。


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

您应该检查是 True 还是 false。

b == true
if b: # If b is True
   do something 

在你的情况下

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

Here https://faisalferoz.wordpress.com/2010/09/16/why-boolean-true-type-of-comparisions-should-be-avoided/一些解释

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

PEP 8:与 True 的比较应该是“if cond is True:”或“if cond:” 的相关文章

随机推荐