Re: Working with a list in a more „pythonic“ way
Nickolay Kolev
nmkolev at uni-bonn.de
Sun Apr 4 10:42:22 EDT 2004
> This is the part I'd change:
>
> # Setup: transitionScore['AF'] = soundScoreMatrix[0][5].
> transitionScore = {}
> for i in range(26):
> first = chr(65+i)
> row = soundScoreMatrix[i]
> for j in range(26):
> transitionScore[first + chr(65+j)] = row[j]
That would create another list containing all possible transitions,
right? Are you doing this just to avoid indexing the matrix later when
going over the phrase letter by letter?
> def scoreit(phrase):
> score = 0
> for i in range(len(phrase) - 1):
> score += transitionScore.get(phrase[i : i+2], 0)
> return score
>
> But if you insist on a more functional style:
>
> def scoreterse(phrase):
> return sum([transitionScore.get(phrase[i : i+2], 0)
> for i in range(len(phrase) - 1)])
It is just those *for i in range(len(x))* things I was hoping to get rid
of.
As mentioned, I have a solution that works and produces the expected
results. I wanted to see if there was a clearer way to write what I had
in mind. You see, writing the factorial function using "reduce" was a
breakthrough for me... :-)
Thanks for your reply!
Nicky
More information about the Python-list
mailing list