python sum over specific indexes 1

python sum over specific indexes

# You can use sum directly after indexing with indices:
a = np.array([1,2,3,4])
indices = [0, 2] 
a[indices].sum()
# Result: 4

Here is what the above code is Doing:
1. a[indices] returns the array [1, 3]
2. .sum() sums the array
3. The result is 4

Similar Posts