Working with a list in a more ?pythonic? way
Hung Jung Lu
hungjunglu at yahoo.com
Sun Apr 4 11:51:20 EDT 2004
Nickolay Kolev <nmkolev at uni-bonn.de> wrote in message news:<20040404124833458+0200 at news.rhrz.uni-bonn.de>...
>
> Having a string consisting of letters only, find out the total sound
> score of the string. The sound score is calculated as the sum of the
> transition scores between the characters in that string. The transition
> scores are stored in a 26 x 26 matrix. I.e. the transition A -> F would
> have the score soundScoreMatrix[0][5].
My attempt here:
import operator
soundScoreMatrix = [[.1,]*26]*26 # sample score matrix
phrase = 'abracadabra'
cap_phrase = phrase.upper()
indices = [ord(c)-65 for c in cap_phrase]
scores = [soundScoreMatrix[indices[i-1]][indices[i]]
for i in range(1, len(indices))]
sum = reduce(operator.add, scores)
print sum
regards,
Hung Jung
More information about the Python-list
mailing list