Dictionaries with tuples or tuples of tuples

Discussion in 'Python' started by Jon Reyes, Feb 19, 2013.

  1. Jon Reyes

    Jon Reyes Guest

    So I have a dictionary and the key is a number. The values are either a single tuple or a tuple of tuples. Is there a better way to go about accessingthe values of the dictionary? All the tuples contain four elements.

    So say:
    col = {"1": (0,1,2,3): "2": ((0,1,2,3),(2,3,4,5))}

    Then to access the values of the tuple I'd do this:

    for key,value in col.iteritems():
    if isinstance(value[0], tuple):
    #iterate through the tuples of a tuple
    else:
    #iterate through the tuple

    At first I was thinking that I could just put the same keys with just single tuples on a dictionary but only one tuple exists when I iterate through the dictionary. I'm sorry, I'm really new at Python and I just grab anythingI can when I need it from Google and the Python docs.
    Jon Reyes, Feb 19, 2013
    #1
    1. Advertising

  2. On 02/18/2013 07:52 PM, Jon Reyes wrote:
    > So I have a dictionary and the key is a number. The values are either a single tuple or a tuple of

    tuples. Is there a better way to go about accessing the values of the
    dictionary? All the tuples contain four elements.
    >
    > So say:
    > col = {"1": (0,1,2,3): "2": ((0,1,2,3),(2,3,4,5))}
    >
    > Then to access the values of the tuple I'd do this:
    >
    > for key,value in col.iteritems():
    > if isinstance(value[0], tuple):
    > #iterate through the tuples of a tuple
    > else:
    > #iterate through the tuple
    >
    > At first I was thinking that I could just put the same keys with just

    single tuples on a dictionary but only one tuple exists when I iterate
    through the dictionary. I'm sorry, I'm really new at Python and I just
    grab anything I can when I need it from Google and the Python docs.

    It would be easier to process if, when adding a single tuple
    to the dict, you could wrap it inside a tuple: (mytup,)

    If your data set is not very large and you don't mind the
    slight performance hit, you can simplify processing step:

    for k,v in col.iteritems():
    if not isinstance(v[0], tuple):
    v = (v,)
    for tup in v: ...


    --
    Lark's Tongue Guide to Python: http://lightbird.net/larks/

    Although the most acute judges of the witches and even the witches
    themselves, were convinced of the guilt of witchery, the guilt nevertheless
    was non-existent. It is thus with all guilt. Friedrich Nietzsche
    Mitya Sirenef, Feb 19, 2013
    #2
    1. Advertising

  3. Jon Reyes

    Roy Smith Guest

    In article <>,
    Jon Reyes <> wrote:

    > So I have a dictionary and the key is a number.
    > [...]
    > col = {"1": (0,1,2,3): "2": ((0,1,2,3),(2,3,4,5))}


    The keys here are strings, not numbers. But that's a detail. Somewhat
    more importantly, that's a syntax error (one of the colons should be a
    comma).

    > The values are either a
    > single tuple or a tuple of tuples. Is there a better way to go about
    > accessing the values of the dictionary? All the tuples contain four elements.


    I would make all the values the same shape, i.e. all lists of tuples:

    col = {"1": [(0,1,2,3)]: "2": [(0,1,2,3),(2,3,4,5)]}

    Then you're always doing the same thing with values when you process
    them.
    Roy Smith, Feb 19, 2013
    #3
  4. Jon Reyes

    Jon Reyes Guest

    Wow, why didn't I think of that. Thanks! I'll try it now. By the way I think I don't need to wrap the single tuples in runtime because I'm declaring that dictionary anyway beforehand and I could just do it right there. I won't be adding elements to the tuple.
    Jon Reyes, Feb 19, 2013
    #4
  5. Jon Reyes

    Jon Reyes Guest

    Wow, why didn't I think of that. Thanks! I'll try it now. By the way I think I don't need to wrap the single tuples in runtime because I'm declaring that dictionary anyway beforehand and I could just do it right there. I won't be adding elements to the tuple.
    Jon Reyes, Feb 19, 2013
    #5
  6. Jon Reyes

    Jon Reyes Guest

    Sorry if I didn't check the code before I posted it, I just mocked it up in Google's editor. That's what Mitya suggested too, yep, I guess I just need to make it uniform to get rid of the extra checking. Thanks man!
    Jon Reyes, Feb 19, 2013
    #6
  7. On 19/02/2013 00:52, Jon Reyes wrote:
    > So I have a dictionary and the key is a number. The values are either a single tuple or a tuple of tuples. Is there a better way to go about accessing the values of the dictionary? All the tuples contain four elements.
    >
    > So say:
    > col = {"1": (0,1,2,3): "2": ((0,1,2,3),(2,3,4,5))}
    >
    > Then to access the values of the tuple I'd do this:
    >
    > for key,value in col.iteritems():
    > if isinstance(value[0], tuple):
    > #iterate through the tuples of a tuple
    > else:
    > #iterate through the tuple
    >
    > At first I was thinking that I could just put the same keys with just single tuples on a dictionary but only one tuple exists when I iterate through the dictionary. I'm sorry, I'm really new at Python and I just grab anything I can when I need it from Google and the Python docs.
    >


    How about this using Python 3.

    col = {"1": ((0,1,2,3),), "2": ((0,1,2,3),(2,3,4,5))}
    for key,tuples in col.items():
    for t in tuples:
    print(key,t)

    A slight aside, your keys look more like strings to me than numbers :)

    --
    Cheers.

    Mark Lawrence
    Mark Lawrence, Feb 19, 2013
    #7
  8. Jon Reyes

    Jon Reyes Guest

    Hi Mark. Well, doesn't iteritems() work the same? or am I missing something? By the way I'm sure I read the dictionaries part of Python but I'm unsureif it would take int's as a key for dictionaries. I've been weaned on Javawhere the keys of hashmaps are always Strings.

    PS: Just checked, wow I could use ints as keys. Awesome!
    Jon Reyes, Feb 19, 2013
    #8
  9. Jon Reyes

    Jon Reyes Guest

    Hi Mark. Well, doesn't iteritems() work the same? or am I missing something? By the way I'm sure I read the dictionaries part of Python but I'm unsureif it would take int's as a key for dictionaries. I've been weaned on Javawhere the keys of hashmaps are always Strings.

    PS: Just checked, wow I could use ints as keys. Awesome!
    Jon Reyes, Feb 19, 2013
    #9
  10. Jon Reyes

    Dave Angel Guest

    On 02/18/2013 08:38 PM, Jon Reyes wrote:
    > Hi Mark. Well, doesn't iteritems() work the same? or am I missing something? By the way I'm sure I read the dictionaries part of Python but I'm unsure if it would take int's as a key for dictionaries. I've been weaned on Java where the keys of hashmaps are always Strings.
    >
    > PS: Just checked, wow I could use ints as keys. Awesome!
    >


    The keys to a dictionary may be any immutable type. That includes str,
    int, and tuple, but it also can include any other class that meets a
    couple of simple criteria. In simplified language, the only requirement
    is that the key object cannot change its value or hash, so that if two
    key objects are equal, they stay equal, and if they differ, they stay
    different.

    --
    DaveA
    Dave Angel, Feb 19, 2013
    #10
  11. On 02/18/2013 08:38 PM, Jon Reyes wrote:
    > Hi Mark. Well, doesn't iteritems() work the same? or am I missing something? By the way I'm

    sure I read the dictionaries part of Python but I'm unsure if it would
    take int's as a key for dictionaries. I've been weaned on Java where the
    keys of hashmaps are always Strings.
    >
    > PS: Just checked, wow I could use ints as keys. Awesome!


    In fact, any hashable object can be a key in a dict, so you can define
    your own custom objects and use them as keys -- this can be
    extremely useful sometimes!

    -m


    --
    Lark's Tongue Guide to Python: http://lightbird.net/larks/

    Oaths are the fossils of piety. George Santayana
    Mitya Sirenef, Feb 19, 2013
    #11
  12. Jon Reyes

    Jon Reyes Guest

    Thanks Dave and Mitya for enlightening me about dictionaries. I'm still confused about this though:

    " so that if two
    key objects are equal, they stay equal, and if they differ, they stay
    different. "

    What does this mean? I won't be comparing key objects with one another. Also, when I had two keys with the same value the value of the other key disappeared so I assume in runtime if there are multiple keys of the same value only the last one will appear.
    Jon Reyes, Feb 19, 2013
    #12
  13. Jon Reyes

    Jon Reyes Guest

    Thanks Dave and Mitya for enlightening me about dictionaries. I'm still confused about this though:

    " so that if two
    key objects are equal, they stay equal, and if they differ, they stay
    different. "

    What does this mean? I won't be comparing key objects with one another. Also, when I had two keys with the same value the value of the other key disappeared so I assume in runtime if there are multiple keys of the same value only the last one will appear.
    Jon Reyes, Feb 19, 2013
    #13
  14. On 19/02/2013 01:38, Jon Reyes wrote:
    > Hi Mark. Well, doesn't iteritems() work the same?
    >


    It's iteritems for Python 2, items for Python 3.

    --
    Cheers.

    Mark Lawrence
    Mark Lawrence, Feb 19, 2013
    #14
  15. Jon Reyes

    Jon Reyes Guest

    Oh, I see, thanks! I was thinking I'll study 2.7 and once I'm comfortable with Python as a language I'll move to 3. Heck, I don't even know how to create a simple main method.
    Jon Reyes, Feb 19, 2013
    #15
  16. Jon Reyes

    Jon Reyes Guest

    Oh, I see, thanks! I was thinking I'll study 2.7 and once I'm comfortable with Python as a language I'll move to 3. Heck, I don't even know how to create a simple main method.
    Jon Reyes, Feb 19, 2013
    #16
  17. On 02/18/2013 09:17 PM, Jon Reyes wrote:
    > Thanks Dave and Mitya for enlightening me about dictionaries. I'm still confused about this though:
    >
    > " so that if two
    > key objects are equal, they stay equal, and if they differ, they stay
    > different. "
    >
    > What does this mean? I won't be comparing key objects with one

    another. Also, when I had two keys with the same value the value of the
    other key disappeared so I assume in runtime if there are multiple keys
    of the same value only the last one will appear.

    You won't be, but dict will.

    Dict is by definition a mapping where a value is assigned to a unique
    key. If you have two keys and two values, and then change one key to
    be equal to the second key, that's not kosher, because which value it's
    supposed to return when you try to get it by that key?

    So in effect, key's hash value should not change. If key is immutable,
    you can be certain that it's hash value will not change. If it's
    mutable, you have to make sure not to change the key in a way that'd
    make its hash value different than it was.

    -m

    --
    Lark's Tongue Guide to Python: http://lightbird.net/larks/

    Graphic design is the paradise of individuality, eccentricity, heresy,
    abnormality, hobbies and humors. George Santayana
    Mitya Sirenef, Feb 19, 2013
    #17
  18. Jon Reyes

    Dave Angel Guest

    On 02/18/2013 09:54 PM, Mitya Sirenef wrote:
    > On 02/18/2013 09:17 PM, Jon Reyes wrote:
    >> Thanks Dave and Mitya for enlightening me about dictionaries. I'm
    >> still confused about this though:
    > >
    > > " so that if two
    > > key objects are equal, they stay equal, and if they differ, they stay
    > > different. "
    > >
    > > What does this mean? I won't be comparing key objects with one

    > another. Also, when I had two keys with the same value the value of the
    > other key disappeared so I assume in runtime if there are multiple keys
    > of the same value only the last one will appear.
    >
    > You won't be, but dict will.
    >
    > Dict is by definition a mapping where a value is assigned to a unique
    > key. If you have two keys and two values, and then change one key to
    > be equal to the second key, that's not kosher, because which value it's
    > supposed to return when you try to get it by that key?
    >
    > So in effect, key's hash value should not change. If key is immutable,
    > you can be certain that it's hash value will not change. If it's
    > mutable, you have to make sure not to change the key in a way that'd
    > make its hash value different than it was.
    >
    > -m
    >


    It's a little stronger than that, since equal hashes cannot assure equal
    data. The equality of each object pair in a dict must not change over
    time, not just the hashes of the individual objects.

    --
    DaveA
    Dave Angel, Feb 19, 2013
    #18
  19. On 02/18/2013 10:14 PM, Dave Angel wrote:
    > On 02/18/2013 09:54 PM, Mitya Sirenef wrote:
    >> On 02/18/2013 09:17 PM, Jon Reyes wrote:
    >>> Thanks Dave and Mitya for enlightening me about dictionaries. I'm
    >>> still confused about this though:
    >> >
    >> > " so that if two
    >> > key objects are equal, they stay equal, and if they differ, they stay
    >> > different. "
    >> >
    >> > What does this mean? I won't be comparing key objects with one

    >> another. Also, when I had two keys with the same value the value of the
    >> other key disappeared so I assume in runtime if there are multiple keys
    >> of the same value only the last one will appear.
    >>
    >> You won't be, but dict will.
    >>
    >> Dict is by definition a mapping where a value is assigned to a unique
    >> key. If you have two keys and two values, and then change one key to
    >> be equal to the second key, that's not kosher, because which value it's
    >> supposed to return when you try to get it by that key?
    >>
    >> So in effect, key's hash value should not change. If key is immutable,
    >> you can be certain that it's hash value will not change. If it's
    >> mutable, you have to make sure not to change the key in a way that'd
    >> make its hash value different than it was.
    >>
    >> -m
    >>

    >
    > It's a little stronger than that, since equal hashes cannot assure
    > equal data. The equality of each object pair in a dict must not
    > change over time, not just the hashes of the individual objects.
    >


    Ah, yes - that's true; if hashes were unequal and then the key is
    changed to be equal to the first key, both mydict[key1] and mydict[key2]
    will give you value1, but iterating over dict items will print key1,
    value1; key2, value2. And that's not a good thing. -m


    --
    Lark's Tongue Guide to Python: http://lightbird.net/larks/

    True friends stab you in the front.
    Oscar Wilde
    Mitya Sirenef, Feb 19, 2013
    #19
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Nickolay Kolev
    Replies:
    3
    Views:
    348
    Scott David Daniels
    Nov 10, 2004
  2. lysdexia
    Replies:
    6
    Views:
    477
    John Machin
    Dec 2, 2007
  3. Brandon
    Replies:
    12
    Views:
    472
    Brandon
    Aug 15, 2008
  4. Chris Angelico
    Replies:
    1
    Views:
    106
    Chris Angelico
    Jan 30, 2013
  5. Mitya Sirenef
    Replies:
    0
    Views:
    94
    Mitya Sirenef
    Jan 30, 2013
Loading...

Share This Page