Reputation: 2221
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.
Upvotes: 1
Views: 566
Reputation: 1819
I see those solutions :
print sum(Score[x] for x in A)
print sum(map(lambda x: Score[x], A))
Upvotes: 2
Reputation:
The Python function "sum" is pretty efficient. It avoids memory overhead (I believe it is written in C) and should be a bit faster.
It would look like this
intSum = sum(array)
Upvotes: 1