forcing dictionary order
Author Message
forcing dictionary order

I'd like (not a must!) to walk a dictionary in the same order in which
it was created. Example:

foo={ key1: (), key2: (), key3: () }

If I do:

for a in foo.keys(): ...

the order is not "key1, key2, key3", which is what I want. Note, I can't
do something simple like sort() on the keys since the are not in any
alphanumeric order. I'm just using this to generate a largish table for
another program, and the actual order doesn't matter for it to work
properly, but it would nicer if it were in the correct logical order.
So, I'm not going to spend a lot of time on this, but if there is a
simple way????

--
Bob van der Poel ** Wynndel, British Columbia, CANADA **

WWW:   http://www.*-*-*.com/ ~bvdpoel

Tue, 16 Sep 2003 02:36:30 GMT
forcing dictionary order

Quote:
> I'd like (not a must!) to walk a dictionary in the same order in which
> it was created. Example:

Subclass UserDict and keep track of additions yourself.

Daniel

Tue, 16 Sep 2003 03:23:40 GMT
forcing dictionary order

Quote:

> I'd like (not a must!) to walk a dictionary in the same order in which
> it was created. Example:

>    foo={ key1: (), key2: (), key3: () }

> If I do:

>    for a in foo.keys(): ...

> the order is not "key1, key2, key3", which is what I want. Note, I can't
> do something simple like sort() on the keys since the are not in any
> alphanumeric order. I'm just using this to generate a largish table for
> another program, and the actual order doesn't matter for it to work
> properly, but it would nicer if it were in the correct logical order.
> So, I'm not going to spend a lot of time on this, but if there is a
> simple way????

Once they are in the dictionary, they keys are hashed, so any info
on the order of insertion is lost.

You can create a Class based on UserDict, and override the __setitem__
method (and to be consistent, __delitem__ and update) to keep an ordered
list of keys in parallel.

You might also redefine popitem to return pairs in the proper order.
( Do you want FIFO or LIFO ? )

-- Steve Majewski

Tue, 16 Sep 2003 03:04:45 GMT
forcing dictionary order

Quote:

> I'd like (not a must!) to walk a dictionary in the same order in
> which it was created. Example:

>    foo={ key1: (), key2: (), key3: () }

> If I do:

>    for a in foo.keys(): ...

> the order is not "key1, key2, key3", which is what I want. Note, I
> can't do something simple like sort() on the keys since the are not
> in any alphanumeric order. I'm just using this to generate a largish
> table for another program, and the actual order doesn't matter for
> it to work properly, but it would nicer if it were in the correct
> logical order. So, I'm not going to spend a lot of time on this, but
> if there is a simple way????

Sure.  Maintain a parallel array of references to the entries in the
dictionary as the dictionary is populated.

--
Bob Kline

http://www.rksystems.com

Tue, 16 Sep 2003 03:46:02 GMT
forcing dictionary order

Quote:
> I'd like (not a must!) to walk a dictionary in the same order in which
> it was created. Example:

>    foo={ key1: (), key2: (), key3: () }

> If I do:

>    for a in foo.keys(): ...

> the order is not "key1, key2, key3", which is what I want. Note, I can't
> do something simple like sort() on the keys since the are not in any
> alphanumeric order. I'm just using this to generate a largish table for
> another program, and the actual order doesn't matter for it to work
> properly, but it would nicer if it were in the correct logical order.
> So, I'm not going to spend a lot of time on this, but if there is a
> simple way????

Dictionaries by themselves don't do this, they don't have any particular
order at all, and forget the order in which the items were inserted.

So you'll have to keep track of it yourself, maybe keep a 'fookeys' list
besides the dict that keeps the keys, in the desired order.

--
Remco Gerlich

Tue, 16 Sep 2003 14:37:12 GMT

 Page 1 of 1 [ 5 post ]

Relevant Pages