Dictionaries with tuples or tuples of tuples

J

Jon Reyes

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.
 
M

Mitya Sirenef

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
 
R

Roy Smith

Jon Reyes said:
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.
 
J

Jon Reyes

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.
 
J

Jon Reyes

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.
 
J

Jon Reyes

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!
 
M

Mark Lawrence

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 :)
 
J

Jon Reyes

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!
 
J

Jon Reyes

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!
 
D

Dave Angel

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.
 
M

Mitya Sirenef

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
 
J

Jon Reyes

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.
 
J

Jon Reyes

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.
 
J

Jon Reyes

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.
 
J

Jon Reyes

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.
 
M

Mitya Sirenef

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
 
D

Dave Angel

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.
 
M

Mitya Sirenef

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top