繁体   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