PEP 8:与True的比较应为'如果cond为True:'或'if cond:'

时间:2018-06-12 11:35:38

标签: 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) 

如果我将temp == True更改为temp,则代码无法按预期工作,如其他帖子所示。

该警告应如何解决?

其中(temp)有效。非常感谢 !! @Joao Vitorino 谢谢你的解释,@ jedwards。它有助于。

2 个答案:

答案 0 :(得分:2)

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

您应该检查是真还是假。

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

在你的情况下

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

Here一些解释

答案 1 :(得分:0)

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

temp = True
pcos = where(temp)

如果将'temp'分配为false,则在条件语句中仅指定'temp'的结果为True。例如:

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

注意:如果您的代码未遵循PEP8,这种情况将不会出现任何错误。

相关问题