Python PEP 8:与True的比较应为';如果cond为True:';或';如果条件:';

Python PEP 8:与True的比较应为';如果cond为True:';或';如果条件:';,python,pycharm,Python,Pycharm,PyCharm在执行np.where(temp==True)时抛出警告 我的完整代码: 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.

PyCharm在执行np.where(temp==True)时抛出警告

我的完整代码:

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为True,代码将无法按预期工作

如何解决此警告

(临时工)工作的地方。非常感谢@若奥·维托里诺
谢谢你的解释,@jedwards。这很有帮助

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

你应该检查是真是假

b == true
if b: # If b is True
   do something 
就你而言

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

一些解释

根据Python中的PEP8指南,将事情与事实进行比较不是首选模式

temp = True
pcos = where(temp)
如果“temp”被指定为false,那么在条件语句中仍然只指定“temp”将导致True。例如:

temp = False
pros = while(temp) # if or while condition

注意:如果您的代码不符合PEP8,则本例不会给出任何错误

pos=其中(temp)同样,您应该理解
x==True
x为True
之间的区别。如果x为True,则前者将计算为True,如(相当于
bool(x)==True
——因此
1==True
“字符串”==True
等都将通过此测试。使用
的后者将测试引用相等性,并且仅与布尔值True完全匹配。如果
temp
是布尔值,或者该函数采用布尔值,只需执行
np。其中(temp)