格式化numpy数组以查找Python中2个值之间的总和



我正在尝试将下面的值修改为预期值。下面的函数用于求出limits的两个连续元素之间的所有值。没有一个值在CCD_ 3内的CCD_。然而,2 and 5之间的值是Numbers内的3,4,因此结果是3+4=7。函数已从issue:sissue获得。

def formating(a, b):

# Formating goes here
x = np.sort(b);
# digitize
l = np.digitize(a, x)
# output:
result = np.bincount(l, weights=a)
return result
Numbers = np.array([3, 4, 5, 7, 8, 10,20])
limit1 = np.array([0, 2 , 5, 12, 15])
limit2 = np.array([0, 2 , 5, 12])
limit3 = np.array([0, 2 , 5, 12, 15, 22])
result1= formating(Numbers, limit1)
result2= formating(Numbers, limit2)
result3= formating(Numbers, limit3)

电流输出

result1:  [ 0.  0.  7. 30.  0. 20.] 
result2:  [ 0.  0.  7. 30. 20.] 
result3:  [ 0.  0.  7. 30.  0. 20.]

想要输出:

result1:  [ 0.  7. 30.  0.] 
result2:  [ 0.  7. 30. ] 
result3:  [ 0.  7. 30.  0. 20.]

所以把末尾的数字扔掉。

result1 = result1[1:len(limit1)]
result2 = result2[1:len(limit2)]
result3 = result3[1:len(limit3)]

或者,为了获得更智能的结果,以结束功能

result = np.bincount(1, weights=a)
return result[1:len(b)]

最新更新