473,703 Members | 4,416 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Why are there no ordered dictionaries?

This is probably a FAQ, but I dare to ask it nevertheless since I
haven't found a satisfying answer yet: Why isn't there an "ordered
dictionary" class at least in the standard list? Time and again I am
missing that feature. Maybe there is something wrong with my programming
style, but I rather think it is generally useful.

I fully agree with the following posting where somebody complains why so
very basic and useful things are not part of the standard library:
http://groups.google.com/group/comp....52d2f771a49857

Are there plans to get it into the standard lib sometime?

-- Christoph
Nov 22 '05
210 10471
Christoph Zwerschke <ci**@online.de > wrote:
...
The 'sorted' function does not help in the case I have indicated, where
"I do not want the keys to be sorted alphabetically, but according to
some criteria which cannot be derived from the keys themselves."


Ah, but WHAT 'some criteria'? There's the rub! First insertion, last
insertion, last insertion that wasn't subsequently deleted, last
insertion that didn't change the corresponding value, or...???
Alex
Nov 22 '05 #41
Christoph Zwerschke <ci**@online.de > wrote:
...
The 'sorted' function does not help in the case I have indicated, where
"I do not want the keys to be sorted alphabetically, but according to
some criteria which cannot be derived from the keys themselves."


Ah, but WHAT 'some criteria'? There's the rub! First insertion, last
insertion, last insertion that wasn't subsequently deleted, last
insertion that didn't change the corresponding value, or...???
Alex
Nov 22 '05 #42

Alex Martelli wrote:
bo****@gmail.co m <bo****@gmail.c om> wrote:
...
there are at least two behaviour. What I need is a "preferred order".
Say if I have designed a web form(correspond to a database table), I
just want say 3 fields that goes before anything else in the
presentation. The rest I don't care as the DBA may create more fields
later which I don't want to then update my code yet again.


preferred_field s = ['foo', 'bar', 'baz']

def process_preferr ed_fields():
global preferred_field s
temp = {}
for i, field in enumerate(prefe rred_fields):
temp[field] = '%s%s' % (chr(0), chr(i))
preferred_field s = temp
process_preferr ed_fields()
del process_preferr ed_fields

def sort_key(akey, preferred_field s=preferred_fie lds):
return preferred_field s.get(akey, akey)
del preferred_field s

## ...build dictionary d...

# now output d...:
for k in sorted(d, key=sort_key):
print k, d[k]

Season to taste if you want non-preferred fields emitted other than
alphabetically, or if you want to wrap this stuff into a class, etc.
(Note: untested code, so typos &c are quite possible). This assumes
that no 'real' key is a non-string, and no 'real' key starts with
chr(0), but it's quite easy to tweak for slightly different specs (at
worst by defining a custom type designed to always compare less than any
real key, and wrapping the preferred_field s entry in instances of that
custom type... having such instances compare with each other based on
the index within preferred_field s of the key they're wrapping, etc etc).

Thanks. For me, I don't need such complex thing, it is just like :

d = somedict_from_d b()
prefer=['f','a',b']

def my_order(d):
for x in prefer:
if x in d: yield x
s = frozenset(prefe r)
for x in d:
if x not in s: yield x

Nov 22 '05 #43
bo****@gmail.co m wrote:
Using the same logic, we don't need types other than string in a DBMS
as we can always convert a string field into some other types when it
is needed.


You mean, like SQLite does? (http://www.sqlite.org/datatypes.html)

-Peter
Nov 22 '05 #44
bo****@gmail.co m wrote:
Using the same logic, we don't need types other than string in a DBMS
as we can always convert a string field into some other types when it
is needed.


You mean, like SQLite does? (http://www.sqlite.org/datatypes.html)

-Peter
Nov 22 '05 #45

Peter Hansen wrote:
bo****@gmail.co m wrote:
Using the same logic, we don't need types other than string in a DBMS
as we can always convert a string field into some other types when it
is needed.


You mean, like SQLite does? (http://www.sqlite.org/datatypes.html)

Yup, they are using similar logic.

Nov 22 '05 #46

Peter Hansen wrote:
bo****@gmail.co m wrote:
Using the same logic, we don't need types other than string in a DBMS
as we can always convert a string field into some other types when it
is needed.


You mean, like SQLite does? (http://www.sqlite.org/datatypes.html)

Yup, they are using similar logic.

Nov 22 '05 #47

Bengt Richter wrote:
On Sun, 20 Nov 2005 22:03:34 +0100, Christoph Zwerschke <ci**@online.de > wrote:
Ordering the keys isn't the normal case, and can be done easily when
needed.


That depends. Maybe I do not want the keys to be sorted alphabetically,
but according to some criteria which cannot be derived from the keys
themselves.

You mean involving also the values? What's wrong with
sorted(plaindic t.items(), key=your_orderi ng_function) ?

Not according to the content of the data, not just the "key". Or in
other words, some other metadata that is not present in the data. A
typical thing, like order of creation. Or some arbitary order. For
example :

I present a data grid/table in a HTML form and the user just drag and
drop and rearrange the columns order.

Of course, you may say, just put another column that represent
this(some reporting programs I have seen do it this way) and that is an
option but not the only option.

Nov 22 '05 #48
bo****@gmail.co m <bo****@gmail.c om> wrote:
[sort by] some other metadata that is not present in the data.
[...]
Of course, you may say, just put another column that represent
this(some reporting programs I have seen do it this way) and that is
an option but not the only option.


It's a pretty good option, and very commonly used. It's known as the
"Schwartzia n transform", or more descriptively, the "Decorate, Sort,
Undecorate" pattern.

<URL:http://aspn.activestat e.com/ASPN/Cookbook/Python/Recipe/52234>

--
\ "Experience is that marvelous thing that enables you to |
`\ recognize a mistake when you make it again." -- Franklin P. |
_o__) Jones |
Ben Finney
Nov 22 '05 #49
bo****@gmail.co m <bo****@gmail.c om> wrote:
[sort by] some other metadata that is not present in the data.
[...]
Of course, you may say, just put another column that represent
this(some reporting programs I have seen do it this way) and that is
an option but not the only option.


It's a pretty good option, and very commonly used. It's known as the
"Schwartzia n transform", or more descriptively, the "Decorate, Sort,
Undecorate" pattern.

<URL:http://aspn.activestat e.com/ASPN/Cookbook/Python/Recipe/52234>

--
\ "Experience is that marvelous thing that enables you to |
`\ recognize a mistake when you make it again." -- Franklin P. |
_o__) Jones |
Ben Finney
Nov 22 '05 #50

This thread has been closed and replies have been disabled. Please start a new discussion.

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.