为什么对双重切片的numpy数组进行分配不起作用? [英] Why does an assignment for double-sliced numpy arrays not work?
本文介绍了为什么对双重切片的numpy数组进行分配不起作用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
为什么以下几行不能按我预期的那样工作?
why do the following lines not work as I expect?
import numpy as np
a = np.array([0,1,2,1,1])
a[a==1][1:] = 3
print a
>>> [0 1 2 1 1]
# I would expect [0 1 2 3 3]
这是虫子"还是有其他推荐的方法?
Is this a 'bug' or is there another recommended way to this?
另一方面,以下工作原理:
On the other hand, the following works:
a[a==1] = 3
print a
>>> [0 3 2 3 3]
干杯,菲利普
推荐答案
看来您根本无法通过像这样的双重切片来完成作业.
It appears you simply can't do an assignment through a double-slice like that.
这可以:
a[numpy.where(a==1)[0][1:]] = 3
这篇关于为什么对双重切片的numpy数组进行分配不起作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文