python 修改array_python numpy更改数组值

As with index arrays, what is returned is a copy of the data, not a view as one gets with slices.Unlike some of the references (such as array and mask indices) assignments are always made to the original data in the array (indeed, nothing else would make sense!).

但你不能在这里这么做。虽然您可以指定给sdd[sdd['name'] == 'song']并修改原始数组,但分配给sdd[sdd['name'] == 'song']['age']就是修改副本。在

要理解原因,您必须了解Python中索引的工作原理:像spam[eggs][beans][toast] = value这样的目标调用spam.__getitem__(eggs).__getitem__(beans).__setitem__(toast, value);NumPy无法分辨出sdd[sdd['name'] == 'song']部分是被查找来分配某个东西而不是访问它的。在

文件解释了NumPy如何处理这个简单的案例:… a new array is extracted from the original (as a temporary) containing the values at 1, 1, 3, 1, then the value 1 is added to the temporary, and then the temporary is assigned back to the original array.

因此,如果有多个[…]索引,则必须手动执行相同的操作:tmp = sdd[sdd['name'] == 'song']

tmp[0]['age'] = 41

sdd[sdd['name'] == 'song'] = tmp

请注意,如果您想一次更新名为song的所有人,而不是只更新第一个(希望只更新?)名为song的人,您只需删除[0],相同的代码就可以工作了。在

但是,如果您确实确定只有一个song,并且只想更新他,那么您可以在xnx's answer中使用基于where的解决方案,这将有效地将包含掩码索引数组的多个索引转换为一个简单的索引。在

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值