473,386 Members | 1,752 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

access dictionary with preferred order ?

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
3 1469
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Tim Daneliuk | last post by:
I am aware that dictionary order is not guaranteed. But I ran into something puzzling today. I filled a dictionary dynamically as a program ran. Up to a certain point, the key order was the...
1
by: Sean Berry | last post by:
If I have a dictionary like the following: {'category' : 2, 'shape', 4} How can I use this to make category = 2 and shape = 4. I want to be able to do this regardless of the dict values...
8
by: Rodd Snook | last post by:
I have an application which makes extensive use of the Scripting.Dictionary object. I'm not doing anything silly like putting them outside the page scope -- just creating quite a few of them and...
1
by: Thats Me | last post by:
TMALSS: Task With maintenance of Access Database I did not develop, don't ask about non-commented code problems or where data dictionary is (LOL). Have table with Inventory data for all...
15
by: Claudio Grondi | last post by:
Let's consider a test source code given at the very end of this posting. The question is if Python allows somehow access to the bytes of the representation of a long integer or integer in...
22
by: Jordan S. | last post by:
SQL Server will be used as the back-end database to a non trivial client application. In question is the choice of client application: I need to be able to speak intelligently about when one...
3
by: craigkenisston | last post by:
I want to use Dictionary class like this: textboxM.Text = oMyDict.GetValue("textboxM"); textboxN.Text = oMyDict.GetValue("textboxN"); Where my dictionary contains the list of values for my...
11
by: John | last post by:
I am coding a radix sort in python and I think that Python's dictionary may be a choice for bucket. The only problem is that dictionary is a mapping without order. But I just found that if the...
7
by: mmm | last post by:
I found code to undo a dictionary association. def undict(dd, name_space=globals()): for key, value in dd.items(): exec "%s = %s" % (key, repr(value)) in name_space So if i run I get
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.