[Tutor] handling tuples in a dictionary

Danny Yoo dyoo at hkn.eecs.berkeley.edu
Mon Sep 29 02:46:50 EDT 2003



> > I have a dictionary with items of the following form:
> >
> > dict = {('x',1): -100, ('y',2):-200, ('x',2):100, ('z',3):150, ('y',
> 4):50}
> >
> > I have tuple as the keys.

Hi Jimmy,

Out of curiosity, would it be ok to change the data structure slightly?

Right now, we're using tuples as our keys, and integers as our values.
The problem might be simpler if we make our keys single letters, and the
values be another dictionary that maps a number to another number.
Something like:

    d = { 'x' : { 1: -100,
                  2, 100},
          'y' : { 2: -200,
                  4, 50},
          'z' : { 3: 150 }
        }

is a possible restructuring of the data.  I have no idea if it's a good
one though --- it depends on what the structure is trying to represent.

The structure above better mimics the output that you're requesting, so
I'm wondering if your program can use this representation.  The problem
still feels a little abstract to me; can you tell us more about what the
single letter, and those numbers stand for in:

    dict = {('x', 1): -100, ('y',2):-200, ('x',2):100, ('z',3):150,
            ('y', 4):50}

What is this data?  I just want to understand what we're trying to do, and
why.  *grin*


Talk to you later!




More information about the Tutor mailing list