Working with a list in a more „pythonic“ way

Nickolay Kolev nmkolev at uni-bonn.de
Sun Apr 4 06:48:28 EDT 2004


Hi all,

I would like to find a more pythonic way of solving the following:

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].

I have come up with the following solution, but I hope some of you might 
suggest a more _functional_ (reduce and map) way of diong it.

n = 0           # the final score of the string

for i in range(len(phrase)):
	try:
		n += soundScoreMatrix[ord(x[i]) - 65][ord(x[i + 1]) - 65]
	except IndexError:
		pass

I was thinking about using "reduce", but that would not work as the 
input and output of the function I would use are different (string input, 
integer output).

Many thanks in advance,
Nicky



More information about the Python-list mailing list