小编fig*_*war的帖子

使用带有冗余元素的python数组索引python数组

我遇到了数组索引的问题.假设您有一个数组a和另一个要用作a的索引的数组b,以便为b元素指向的位置分配一些值.

a=numpy.zeros(5)
print a

[ 0.  0.  0.  0.  0.]
Run Code Online (Sandbox Code Playgroud)

现在我想两次增加第二个元素

b=numpy.array([1,1])
a[b]+=1.
print a

[ 0.  1.  0.  0.  0.]
Run Code Online (Sandbox Code Playgroud)

虽然我期望有

[ 0.  2.  0.  0.  0.] 
Run Code Online (Sandbox Code Playgroud)

如果阵列b没有冗余(其元素的所有值都不同),则没有问题.有人为这样的问题找到了解决方案,避免使用for循环吗?这是一个numpy的错误吗?提前致谢

python arrays indexing numpy scipy

4
推荐指数
1
解决办法
247
查看次数

标签 统计

arrays ×1

indexing ×1

numpy ×1

python ×1

scipy ×1