为什么Python在创建列表时解释减法时不一致?

发布于 2024-09-18 05:25:40 字数 1422 浏览 1 评论 0原文

我正在制作一个小程序,在某个时刻,我需要从矩阵的每一行中减去该行本身的平均值。相当标准的重正化过程。

注意代码中的

def subtractaverage(data):
    datanormalized=[]
    for row in data:
        average_row=sum(row)/len(row)
        print "average=",average_row
#       renormalized_row=[cell-average_row for cell in row]
        renormalized_row=[-average_row+cell for cell in row]        
        datanormalized.append(renormalized_row) 
    matrixnormalized=np.array(datanormalized)
    return matrixnormalized

行: # renormalized_row=[行中单元格的单元格平均行] renormalized_row=[-average_row+cell for cell in row]

我首先尝试了第一行(cell-average_row),但它不起作用。结果是 renormalized_row 最终等于 row。

然后第二行就起作用了。所以不知何故,编译器似乎将 [cell-average_row for cell in row] 解释为 [cell for cell in row]。

但是如果我写:

renormalized_row=[cell-100 for cell in row] 

它工作正常(并生成一个新列表,其中每个单元格减去值 100。我尝试了另一个小程序,然后:

rs=range(10)
val=5
t=[r-val for r in rs]
print t,rs

这也可以工作并生成

[-5, -4, -3, -2, -1, 0, 1, 2, 3, 4] [0、1、2、3、4、5、6、7、8、9]

应该如此。

所以现在我很茫然。 是的,我可以使用 renormalized_row=[-average_row+行中单元格的单元格] 但我想了解发生了什么事。为什么表达式的解释方式明显不一致。

我在 OSX 10.6.4 上使用 python2.6.5(2.6.6 没有适用于 Mac 的 .dmg)

谢谢,

当天晚些时候尝试该程序,在另一组数据上,它实际上有效。在原始数据上再次测试它再次工作。我更困惑了。但我知道,甚至错过了宣战理由来表明某些事情没有发挥应有的作用。

我们可以结束这个问题吗

I am making a small program and at some point from each row of a matrix I need to subtract the average of the row itself. Quite a standard renormalization procedure.

Note in the code

def subtractaverage(data):
    datanormalized=[]
    for row in data:
        average_row=sum(row)/len(row)
        print "average=",average_row
#       renormalized_row=[cell-average_row for cell in row]
        renormalized_row=[-average_row+cell for cell in row]        
        datanormalized.append(renormalized_row) 
    matrixnormalized=np.array(datanormalized)
    return matrixnormalized

The lines:
# renormalized_row=[cell-average_row for cell in row]
renormalized_row=[-average_row+cell for cell in row]

I first tried the first line (cell-average_row) and it did NOT work. The result was that renormalized_row ended up being equal to row.

Then the second line instead worked. SO somehow it seem that the compiler is interpreting [cell-average_row for cell in row] as [cell for cell in row].

But if I write:

renormalized_row=[cell-100 for cell in row] 

it works fine (and produces a new list with the value 100 subtracted from each cell. I tried another small program, then:

rs=range(10)
val=5
t=[r-val for r in rs]
print t,rs

This also works and produces

[-5, -4, -3, -2, -1, 0, 1, 2, 3, 4]
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

as it should.

So now I am at a loss.
Yes I can use
renormalized_row=[-average_row+cell for cell in row]
but I would like to understand what is going on. Why this apparent inconsistency in the way the expression is interpreted.

I am using python2.6.5 (2.6.6 won't have a .dmg for Mac) on a OSX 10.6.4

Thanks

Trying the program later the day, on another sets of data, it actually worked. Testing it again on the original data it works again. I am even more confused. But I know even miss the casus belli to show that something was not working as it should.

Can we please close this question

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

耀眼的星火 2024-09-25 05:25:40

我猜问题是整数除法(如果 row 仅由整数组成),

average_row=sum(row)/len(row)

如果行的长度大于总和,则平均值将为 0。尝试

average_row=sum(row)/float(len(row))

一下。

I guess the problem is the integer division (if row consists of integers only)

average_row=sum(row)/len(row)

which will give you an average of 0 if the length of the row is greater than the sum. Try

average_row=sum(row)/float(len(row))

instead.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文