如果条件没有按预期返回

时间:2016-04-05 20:45:10

标签: python python-2.7

我试图写一段代码,只有当列表的所有元素(guess3)都是>时才能找到变量的最小值(RSS3)。我已经完成了:

 if RSS3_min < RSS3_fin and (all(guess3[q]) > 0 for q in range(len(guess3))):
     RSS3_fin = RSS3_min
     guess3_fin = guess3
     print i,j,k, 'RSS3 = ',RSS3_fin, 'guess3 = ', guess3

输出如下:

0 1 2 RSS3 =  152.470584348 guess3 =  [  1.03993316e-18  -4.21309327e-21   3.91958921e-23]
0 1 3 RSS3 =  138.828305634 guess3 =  [ -9.72979812e-18   2.45455199e-20   9.91015993e-17]
0 2 6 RSS3 =  110.971076196 guess3 =  [  2.51690683e-18   6.50243486e-23  -1.64840584e-18]
0 2 12 RSS3 =  110.167564411 guess3 =  [  1.67526232e-18   6.93897310e-23  -9.25020238e-19]

这意味着我if条件的第二部分并不满足。我哪里错了?请考虑一下我对python真的很新,所以请保持简单......谢谢!

1 个答案:

答案 0 :(得分:2)

右括号不在正确的位置,修复它:

if RSS3_min < RSS3_fin and all(guess3[q] > 0 for q in range(len(guess3))):

请注意,由于这是Python,您可以直接遍历列表中的项目:

if RSS3_min < RSS3_fin and all(item > 0 for item in guess3):