[Tutor] Update values stored as a list in a dictionary with values from another dictionary

Kent Johnson kent37 at tds.net
Tue Oct 2 13:26:05 CEST 2007


GTXY20 wrote:
> 
> This seemed to work:
> 
> def transFn(c):
>     transfile = open('translate.txt', 'r')
>     records = transfile.read()
>     transfile.close()
>     lines = records.split()
>     transDict = {}
>     for line in lines:
>         key, value = line.split(',')
>         transDict[key] = value
>     try:
>        return transDict[c]
>     except KeyError:
>        return c

Yikes! This is re-reading translate.txt every time transFn() is called, 
i.e. once for every key in data!

Kent

> 
> for key in data.keys():
>     data[key] = map(transFn, data[key])



More information about the Tutor mailing list