By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,994 Members | 1,166 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,994 IT Pros & Developers. It's quick & easy.

access dictionary with preferred order ?

P: n/a
Hi,

I am wondering if there is a dictionary data type that allows me to
define the order of access when iterating it using items/keys etc. ?

An example:

a=dict(a=dict(), c=dict(), h=dict())
prefer=['e','h', 'a']

for x in a.values: print x

would give me
{h:dict()}, {a:dict()}, then the rest which I don't care about the
order ?

Oct 22 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
This is an "ordered dict":

http://aspn.activestate.com/ASPN/Coo.../Recipe/438823

On Friday 21 October 2005 21:18, bo****@gmail.com wrote:
Hi,

I am wondering if there is a dictionary data type that allows me to
define the order of access when iterating it using items/keys etc. ?

An example:

a=dict(a=dict(), c=dict(), h=dict())
prefer=['e','h', 'a']

for x in a.values: print x

would give me
{h:dict()}, {a:dict()}, then the rest which I don't care about the
order ?


--
James Stroud
UCLA-DOE Institute for Genomics and Proteomics
Box 951570
Los Angeles, CA 90095

http://www.jamesstroud.com/
Oct 22 '05 #2

P: n/a
thanks. But I do quite understand how to specific my order(which could
be arbitary). It seems to have a move method so I need to first loop
through my order list and match then move things around ?

James Stroud wrote:
This is an "ordered dict":

http://aspn.activestate.com/ASPN/Coo.../Recipe/438823

On Friday 21 October 2005 21:18, bo****@gmail.com wrote:
Hi,

I am wondering if there is a dictionary data type that allows me to
define the order of access when iterating it using items/keys etc. ?

An example:

a=dict(a=dict(), c=dict(), h=dict())
prefer=['e','h', 'a']

for x in a.values: print x

would give me
{h:dict()}, {a:dict()}, then the rest which I don't care about the
order ?


--
James Stroud
UCLA-DOE Institute for Genomics and Proteomics
Box 951570
Los Angeles, CA 90095

http://www.jamesstroud.com/


Oct 22 '05 #3

P: n/a
bo****@gmail.com wrote:
I am wondering if there is a dictionary data type that allows me to
define the order of access when iterating it using items/keys etc. ?

An example:

a=dict(a=dict(), c=dict(), h=dict())
prefer=['e','h', 'a']

for x in a.values: print x

would give me
{h:dict()}, {a:dict()}, then the rest which I don't care about the
order ?


a straight-forward implementation should be pretty efficient:

for k in prefer:
try:
v = d.pop(k)
except KeyError:
pass
else:
... deal with k, v ...
for k, v in d.iteritems():
... deal with k, v ...

or, non-destructive:

for k in prefer:
try:
v = d[k]
except KeyError:
pass
else:
... deal with k, v ...
for k, v in d.iteritems():
if k not in prefer:
... deal with k, v ...

the latter is trivial to convert into a helper generator:

def ordered_items(d, prefer):
for k in prefer:
try:
v = d[k]
except KeyError:
pass
else:
yield k, v
for k, v in d.iteritems():
if k not in prefer:
yield k, v

for k, v in ordered_items(d, prefer):
... deal with k, v ...

if you insist on having this behaviour in a type rather than a helper,
subclass dict and add ordered_items as a method.

</F>

Oct 22 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.