python - NumPy 用于设置行和列的索引

标签 python numpy indexing

我有一个 NumPy 零矩阵:

import numpy as np

z = np.zeros((3,3), dtype=int)  # array([[0, 0, 0],
                                #        [0, 0, 0],
                                #        [0, 0, 0]])

接下来,我有一些索引和一些值:

idx = np.array([[0, 0],
                [1, 1],
                [1, 2],
                [2, 2]])

v = np.array([1, 3, 7, 2])

是否有任何 NumPy 函数可以将索引值设置为 my 值?得到如下:

z = array([[1, 0, 0],
           [0, 3, 7],
           [0, 0, 2]])

最佳答案

我相信您想要花哨的索引

n[idx[:,0], idx[:,1]] = v

array([[1, 0, 0],
       [0, 3, 7],
       [0, 0, 2]])

关于python - NumPy 用于设置行和列的索引,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51489580/

相关文章:

Python 控制台不允许我编写新的代码行。 PyCharm

python - 如何处理延时冲突?

python - 将数据从 MySQL 数据库导入到 Pandas 数据框中,包括列名

indexing - Google 网站管理员工具网站未编入索引

python - 属性错误: 'pygame.Rect' object has no attribute 'rect' error when doing collision

python - Pyramid :php 的 $_FILES ['Filedata' ] ['file_name' ] 的类比是什么?

python - 稀疏 hstack 和奇怪的 dtype 转换错误

python - 如何从相关矩阵和波动率向量创建协方差矩阵?

mysql - 聊天 SQL 表的快速索引

python - 如何选择n维数组中的值