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

GTXY20 gtxy20 at gmail.com
Tue Oct 2 18:35:12 CEST 2007


Here's an interesting question:

Can I use the transFn function to remove items in the value list.

Can this be done by simple assigning the current value a value of null
in the translate file?

M.



On 10/2/07, GTXY20 <gtxy20 at gmail.com> wrote:
> I adjusted so that I get the following so if I do not need to translate a
> dictionary I do not call the function transFn:
>
> def transFn(translatefile):
>     transfile = open(translatefile, 'r')
>     records = transfile.read()
>     transfile.close()
>     lines = records.split()
>     transDict = {}
>     for line in lines:
>         key, value = line.split(',')
>         transDict[key] = value
>
>     for key, value in Data.items():
>       Data[key] = [ transDict.get(i, i) for i in value ]
>
> At this point can anyone recommend any python modules that helps to create
> reporting and graphing?
>
> On 10/2/07, Kent Johnson <kent37 at tds.net> wrote:
> >
> > 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