Re: Dictionaries with tuples or tuples of tuples

2013-02-18 Thread Mitya Sirenef

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

--
http://mail.python.org/mailman/listinfo/python-list


Re: Dictionaries with tuples or tuples of tuples

2013-02-18 Thread Dave Angel

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
--
http://mail.python.org/mailman/listinfo/python-list


Re: Dictionaries with tuples or tuples of tuples

2013-02-18 Thread Mitya Sirenef

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

--
http://mail.python.org/mailman/listinfo/python-list


Re: Dictionaries with tuples or tuples of tuples

2013-02-18 Thread 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.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Dictionaries with tuples or tuples of tuples

2013-02-18 Thread Mark Lawrence

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

--
http://mail.python.org/mailman/listinfo/python-list


Re: Dictionaries with tuples or tuples of tuples

2013-02-18 Thread 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.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Dictionaries with tuples or tuples of tuples

2013-02-18 Thread Dave Angel

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
--
http://mail.python.org/mailman/listinfo/python-list


Re: Dictionaries with tuples or tuples of tuples

2013-02-18 Thread Mitya Sirenef

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

--
http://mail.python.org/mailman/listinfo/python-list


Re: Dictionaries with tuples or tuples of tuples

2013-02-18 Thread 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 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!
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Dictionaries with tuples or tuples of tuples

2013-02-18 Thread Mark Lawrence

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

--
http://mail.python.org/mailman/listinfo/python-list


Re: Dictionaries with tuples or tuples of tuples

2013-02-18 Thread Roy Smith
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.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Dictionaries with tuples or tuples of tuples

2013-02-18 Thread 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!
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Dictionaries with tuples or tuples of tuples

2013-02-18 Thread 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.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Dictionaries with tuples or tuples of tuples

2013-02-18 Thread Mitya Sirenef

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

--
http://mail.python.org/mailman/listinfo/python-list


Dictionaries with tuples or tuples of tuples

2013-02-18 Thread 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 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.
-- 
http://mail.python.org/mailman/listinfo/python-list