簡體   English   中英

使用另一個數組創建一個由 1 和 0 組成的數組

[英]Creating a array of 1s and 0s using another array

所以我有兩個 arrays (行和列) row = [0, 1, 0, 2, 0, 1, 3, 1, 3, 1, 2, 4]column = [0, 0, 1, 1, 2, 2, 2, 3, 3, 0, 1, 4]

我想使用行和列 arrays 將值“1”插入另一個二維數組(向量)

vectors = 
[[0. 0. 0. 0. 0.],
[0. 0. 0. 0. 0.],
[0. 0. 0. 0. 0.],
[0. 0. 0. 0. 0.],
[0. 0. 0. 0. 0.]]

所以我想要的 output 是:

vectors = 
[[1. 1. 1. 0. 0.],
[1. 0. 1. 1. 0.],
[0. 1. 0. 0. 0.],
[0. 0. 1. 1. 0.],
[0. 0. 0. 0. 1.]]

抱歉,如果我的解釋不好,這是我第一次使用 python 和 Stackoverflow。

我想你可以試試這個:

for r, c in zip(row, column):
    vectors[r][c] = 1

只需使用rowcolumn向量作為索引:

>>> vectors[row, column] = 1
>>> vectors
array([[1., 1., 1., 0., 0.],
       [1., 0., 1., 1., 0.],
       [0., 1., 0., 0., 0.],
       [0., 0., 1., 1., 0.],
       [0., 0., 0., 0., 1.]])

暫無
暫無

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

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