Working with a list in a more „pythonic“ way

Josiah Carlson jcarlson at uci.edu
Sun Apr 4 14:28:38 EDT 2004


>>     # 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?

He's creating a dictionary.  And yes, he's using it to avoid indexing 
into the matrix.  When you are dealing with some sort of object that is 
indexed by pairs (or triples or quads) of objects, it is quite Pythonic 
to index that object by those same blocks.

 >>> import this
The Zen of Python, by Tim Peters
             .
             .
             .
Flat is better than nested.
             .
             .
             .
 >>>


  - Josiah



More information about the Python-list mailing list