将值分配给numpy.array [英] Assign values to numpy.array

查看:97
本文介绍了将值分配给numpy.array的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

让我们创建这个np.array:

Let say we create this np.array:

A = np.arange(12).reshape(3, 4)

所以,A是:

array([[ 0,  1,  2,  3],
       [ 4,  5,  6,  7],
       [ 8,  9, 10, 11]])

我想像这样为A分配一些值:

I would like to assign some values to A like that:

A[[0,2]][:,[1,3]] = 9999

为了获得:

array([[ 0,  9999,  2,  9999],
       [ 4,  5,  6,  7],
       [ 8,  9999, 10, 9999]])

但这不起作用.正确的方法是什么?

But this doesn't work. What is the proper way to do it?

谢谢

推荐答案

您可以使用 np.ix_ 来获取那些开放的网格物体,当用于索引数组时将是

You can use np.ix_ to get those open meshes, which when used for indexing into the array would be broadcasted and thus could be used for assigning values into it, like so -

A[np.ix_([0,2],[1,3])] = 9999

这篇关于将值分配给numpy.array的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆