access dictionary with preferred order ?

B

bonono

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

James Stroud

This is an "ordered dict":

http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/438823

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/
 
B

bonono

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 said:
This is an "ordered dict":

http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/438823

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/
 
F

Fredrik Lundh

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>
 

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

Forum statistics

Threads
474,269
Messages
2,571,349
Members
48,028
Latest member
Rigor4

Latest Threads

Top