'How do I execute numpy.sum() on arbitrary number of indices or lists in a Tuple?

B = [[1, -2, -3], [2, 5, -7], [1, 6, 5], [1, 0,-1], [-5, 2, 9]]
B[0] = np.sum(B,1)
print(B[0])
>>> [-1, 13, 6]

However, I want to perform sum over 3 indices, i.e., to express B[0] as sum() of B[1], B[2], B[3]. For generality. Therefore, I humbly request if anyone could kindly tell me, with what sort of command or module I could execute such sum() over arbitrary number of lists in a tuple?



Solution 1:[1]

OK, assuming your example up there is showing us what you WANT to receive (because that's NOT what that code produces), what you want is:

B[0] = np.sum(B[1:],0)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1