Re: Working with a list in a more pythonic way
gabriele renzi
surrender_it at remove.yahoo.it
Sun Apr 4 10:11:02 EDT 2004
il 4 Apr 2004 10:48:28 GMT, Nickolay Kolev <nmkolev at uni-bonn.de> ha
scritto::
>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).
why not? you just need to say to reduce to use a 0 as the first
parameter:
say:
def get_val(x):
# gets the value from SSMatrix, now dumb
return ord(x)
reduce(lambda a,b: a+get_val(b),list('blablabla'),0) #909
More information about the Python-list
mailing list