如何从numpy的结构化阵列修改选定行的一列 [英] How to modify one column of a selected row from a numpy structured array

查看:848
本文介绍了如何从numpy的结构化阵列修改选定行的一列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要寻找一个简单的方法来修改它的选线的结构numpy的阵列的一个领域。这里是我的SWE:

I am looking for an easy way to modify one field of a numpy structured array of a selected line of it. Here is my SWE :

import numpy as np
dt=np.dtype([('name',np.unicode,80),('x',np.float),('y',np.float)])
a=np.array( [('a',0.,0.),('b',0.,0.),('c',0.,0.) ],dtype=dt)
b=a.copy()
a[a['name']=='a']['x']=1
print a==b # return [ True  True  True]

在这个例子中, A == b 结果应该返回 [假真真] 。其实,我想选我的阵列线从名称字段,并修改它的一个字段的值(这里是'X')

In this example, the a==b results should return [False True True].Actually, I would like to selected the line of my array from the 'name' field and modify the value of one field of it (here 'x').

推荐答案

我找到了答案......问题的关键是外地和面膜的位置。您需要将面膜适用于字段列,不找蒙面阵列领域:

I found the answer... The point is the position of the field and of the mask. You need to apply the mask to the field column, not looking for the field of the masked array :

a['x'][a['name']=='a']=1
print a==b # returns [False  True  True]

这篇关于如何从numpy的结构化阵列修改选定行的一列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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