Let's say I have a list "A" of length about 40, consisting of integers from 0 to 5, and a list "Score". I want to calculate the sum of Score[A[i]].
Of course, I can do:
sum = 0
for x in A do:
sum += Score[x]
But is there a faster way? I know that numpy can do multiplication of lists, but this requires some sort of indexing.
sum(Score[A[i]])total = sum(Score[x] for x in A)sum()is a builtin function.Scoreis a NumPy array you'd useScore[A].sum(). No particular reason to use this over native Python methods if you don't need other NumPy functions though.