Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Dictionaries with tuples or tuples of tuples

Reply
Thread Tools

Dictionaries with tuples or tuples of tuples

 
 
Jon Reyes
Guest
Posts: n/a
 
      02-19-2013
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.
 
Reply With Quote
 
 
 
 
Mitya Sirenef
Guest
Posts: n/a
 
      02-19-2013
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

 
Reply With Quote
 
 
 
 
Roy Smith
Guest
Posts: n/a
 
      02-19-2013
In article <(E-Mail Removed)>,
Jon Reyes <(E-Mail Removed)> 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.
 
Reply With Quote
 
Jon Reyes
Guest
Posts: n/a
 
      02-19-2013
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.
 
Reply With Quote
 
Jon Reyes
Guest
Posts: n/a
 
      02-19-2013
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.
 
Reply With Quote
 
Jon Reyes
Guest
Posts: n/a
 
      02-19-2013
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!
 
Reply With Quote
 
Mark Lawrence
Guest
Posts: n/a
 
      02-19-2013
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

 
Reply With Quote
 
Jon Reyes
Guest
Posts: n/a
 
      02-19-2013
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!
 
Reply With Quote
 
Jon Reyes
Guest
Posts: n/a
 
      02-19-2013
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!
 
Reply With Quote
 
Dave Angel
Guest
Posts: n/a
 
      02-19-2013
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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: Please provide a better explanation of tuples and dictionaries Mitya Sirenef Python 0 01-30-2013 03:51 AM
Re: Please provide a better explanation of tuples and dictionaries Chris Angelico Python 1 01-30-2013 03:51 AM
updating dictionaries from/to dictionaries Brandon Python 12 08-15-2008 12:35 AM
Pickling dictionaries containing dictionaries: failing,recursion-style! lysdexia Python 6 12-02-2007 12:03 AM
Combine two dictionaries into a list of 3-tuples Nickolay Kolev Python 3 11-10-2004 09:25 PM



Advertisments