Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Python (http://www.velocityreviews.com/forums/f43-python.html)
-   -   Keys order in dictionaries (http://www.velocityreviews.com/forums/t318893-keys-order-in-dictionaries.html)

Brainwashed 06-26-2003 11:26 PM

Keys order in dictionaries
 

Is there any order in dictionaries that will never change ? I've noticed that
assigning always the same elements to the dict puts them always in the same
order. Like regexp which takes 3 values, 'year', 'month', 'day' - I always
get this order:

'year': .., 'day':.., 'month':..

No idea why this order tho.. :) Is there any philosophy in this ?
And if I add only one new value to this regexp, so that it takes 4 now the
order changes totally, but still remains the same for those 4 particular
key-names. If I add 'id' to the previous dict I get:

'year': .., 'id':.., 'day':.., 'month':..

And every time regexp matches those values order is still the same.
Can somebody explain this matter or give some urls to pages where I can
read about this.
And the second thing that bothers me is assigning multiple values at once
from dictionary. I mean:

x, y, z = dict # where dict = { 'xx':.., 'yy':.., 'zz':.. }

This works okay for me, if I know that dict is always of size 3 and there
is the same order of keys:values.

But now I came to the point when this dict can have both - 3 or 4 items
(same as this example from above, 'id' is the additional item that is not
always present). And I want to assign x, y, z like I did before, but ommit
id value. Is it possible to do at once, with some multiple assignment ?
Maybe there is a way to specify what three items should be taken from dict
and assigned to x, y, z? I know, I can always do something like:
x = dict['xx']
y = dict['yy'] and so on, but I'm just curious if some more complicated
multiple assignment is possible in Python.

How would you solve this problem?

/Vald


Ben Finney 06-26-2003 11:50 PM

Re: Keys order in dictionaries
 
On Fri, 27 Jun 2003 01:26:16 +0200, Brainwashed wrote:
> Is there any order in dictionaries that will never change ?


No. The specification for the built-in dictionary type specifically
disclaims any preservation of order, to allow the implementation of any
arbitrary dictionary algorithm, with different, unpredictable (to the
user) storage and retrieval orders.

You can get ordered dictionaries (see the Python cookbook article
already pointed to), but not by directly using the built-in dictionary
type.

--
\ "I have yet to see any problem, however complicated, which, |
`\ when you looked at it in the right way, did not become still |
_o__) more complicated." -- Paul Anderson |
http://bignose.squidly.org/ 9CFE12B0 791A4267 887F520C B7AC2E51 BD41714B

Duncan Booth 06-27-2003 08:53 AM

Re: Keys order in dictionaries
 
Brainwashed <vald@valis.amber.eu.org> wrote in
news:mailman.1056670245.3458.python-list@python.org:

> Is there any order in dictionaries that will never change ? I've
> noticed that assigning always the same elements to the dict puts them
> always in the same order. Like regexp which takes 3 values, 'year',
> 'month', 'day' - I always get this order:
>
> 'year': .., 'day':.., 'month':..
>
> No idea why this order tho.. :) Is there any philosophy in this ?
>


Just because you never saw the order change doesn't mean it won't change.
The code below shows just how easy it is to force the order to change:

>>> d = { 'year': 1, 'month': 2, 'day': 3 }
>>> d

{'month': 2, 'day': 3, 'year': 1}
>>> for i in range(30): d[i] = i


>>> for i in range(30): del d[i]


>>> d

{'month': 2, 'year': 1, 'day': 3}
>>> dict(d)

{'year': 1, 'day': 3, 'month': 2}
>>>


(BTW, that last one surprised me, I had expected dict(d) simply to restore
the original order instead of producing a third ordering).

--
Duncan Booth duncan@rcp.co.uk
int month(char *p){return(124864/((p[0]+p[1]-p[2]&0x1f)+1)%12)["\5\x8\3"
"\6\7\xb\1\x9\xa\2\0\4"];} // Who said my code was obscure?


All times are GMT. The time now is 09:40 AM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.