簡體   English   中英

為數組設置新值會得到與應有的結果不同的結果

[英]Setting a new value to an array gives different results than it should

我有這個數組:

L = np.array([[5,4],[3,2]])

我想更改第二個括號。

如果我做:

print(L[1] + (-0.6)*L[0])

它會給我這個數組:

[0,-0.4]

但是,如果我這樣更改它:

L[1] = L[1] + (-0.6)*L[0]
print(L)

它打印:

# [[5 4] [0 0]]

為什么輸出不同?

第一個例子:

L[1] + (-0.6)*L[0]

返回指定操作的結果,但未分配結果,因此只得到結果。

在第二個例子中

L[1] = L[1] + (-0.6)*L[0]

結果將寫入L[1] ,但是結果將轉換為Ldtype 是整數。 因此,中間結果只是被截斷了。

此“截斷”是結果不“正確”的原因:

>>> (L[1] + (-0.6)*L[0]).astype(int)  # simulating the truncation
array([0, 0])

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM