簡體   English   中英

如何互相減去numpy數組中的相鄰向量

[英]How to subtract neighbouring vectors in a numpy array from each other

我的預期結果如下:

array = [[2,3,4], [1,2,4]]

輸出:

[1, 1, 0]  # [2-1, 3-2, 4-4]

我嘗試通過枚舉並讓索引減去沒有運氣的方式來這樣做:

for i, k in enumerate(array):
    for j in k:
        return(j[i+1] - j[i])

這給了我:

IndexError:標量變量的索引無效。

這有效:

result = [(i-j) for (i,j) in zip(*array)]

輸出:

print (result)
[1, 1, 0]

說明:

zip(*array)等效於元組列表[(2,1), (3,2), (4,4)]

暫無
暫無

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

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