gpt4 book ai didi

python - Numpy 高级索引 : same index used multiple times in +=

转载 作者:行者123 更新时间:2023-11-28 22:24:45 25 4
gpt4 key购买 nike

<分区>

假设您有以下代码

a = np.ones(8)
pos = np.array([1, 3, 5, 3])

a[pos] # returns array([ 1., 1., 1., 1.]), where the 2nd and 4th el are the same
a[pos] +=1

最后一条指令返回

array([ 1.,  2.,  1.,  2.,  1.,  2.,  1.,  1.])

但我希望对相同索引的赋值求和,以便获得

array([ 1.,  2.,  1.,  3.,  1.,  2.,  1.,  1.])

有人遇到过同样的情况吗?

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com