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

GTXY20 gtxy20 at gmail.com
Tue Oct 2 10:31:15 CEST 2007


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

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

On 10/2/07, GTXY20 <gtxy20 at gmail.com> wrote:
>
>
> Sorry - solved my own problem - it was the way I was creating my
> dictionary and assigning the value as a list.
>
> I will post my final working code shortly.
>
> M.
>
> On 10/2/07, GTXY20 <gtxy20 at gmail.com> wrote:
> >
> > I seem to be encountering a problem and I think it is because I actually
> > have my data as follows:
> >
> > data = {1:[a,b,c], 2:[a,c], 3:[b,c], 4:[a,d]}
> >
> > not as previously mentioned:
> >
> > data = {1:(a,b,c), 2:(a,c), 3:(b,c), 4:(a,d)}
> >
> > So the values are actually stored as a list.
> >
> > I am trying to adjust so that data ends up being:
> >
> > {1:[1,2,3], 2:[1,3], 3:[2,3], 4:[1,d]}
> >
> > right now I am getting:
> >
> > {1:[[1],[2],[3]], 2:[[1],[3]], 3:[[2],[3]], 4:[[1],d]}
> >
> > which is problmatic for other things I am trying to do - it is
> > indicating that the values are not hashable.
> >
> >
> >
> >
> >
> > On 10/2/07, John Fouhy <john at fouhy.net> wrote:
> > >
> > > On 02/10/2007, GTXY20 < gtxy20 at gmail.com> wrote:
> > > > Hello all,
> > > >
> > > > Let's say I have the following dictionary:
> > > >
> > > > {1:(a,b,c), 2:(a,c), 3:(b,c), 4:(a,d)}
> > > >
> > > > I also have another dictionary for new value association:
> > > >
> > > > {a:1, b:2, c:3}
> > > >
> > > > How should I approach if I want to modify the first dictionary to
> > > read:
> > > >
> > > >  {1:(1,2,3), 2:(1,3), 3:(2,3), 4:(1,d)}
> > > >
> > > > There is the potential to have a value in the first dictionary that
> > > will not
> > > > have an update key in the second dictionary hence in the above
> > > dictionary
> > > > for key=4 I still have d listed as a value.
> > >
> > > You could use the map function...
> > >
> > > Let's say we have something like:
> > >
> > > transDict = { 'a':1, 'b':2, 'c':3 }
> > >
> > > We could define a function that mirrors this:
> > >
> > > def transFn(c):
> > >     try:
> > >         return transDict[c]
> > >     except KeyError:
> > >         return c
> > >
> > > Then if you have your data:
> > >
> > > data = { 1:('a','b','c'), 2:('a','c'), 3:('b','c'), 4:('a','d')}
> > >
> > > You can translate it as:
> > >
> > > for key in data.keys():
> > >     data[key] = map(transFn, data[key])
> > >
> > > HTH!
> > >
> > > --
> > > John.
> > >
> >
> >
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.python.org/pipermail/tutor/attachments/20071002/d969b949/attachment.htm 


More information about the Tutor mailing list