Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Python (http://www.velocityreviews.com/forums/f43-python.html)
-   -   sort a dictionary by keys in specific order (http://www.velocityreviews.com/forums/t358090-sort-a-dictionary-by-keys-in-specific-order.html)

 spohle 05-26-2006 05:26 PM

sort a dictionary by keys in specific order

hi i have a normal dictionary with key and value pairs. now i wanna
sort by the keys BUT in a specific order i determine in a list !? any
ideas

dic = {'key1':'value1', 'key2':'value2', 'key3':'value3'}

list = [key2, key3, key1]

 Paul Rubin 05-26-2006 05:36 PM

Re: sort a dictionary by keys in specific order

"spohle" <spohle@gmail.com> writes:
> dic = {'key1':'value1', 'key2':'value2', 'key3':'value3'}
>
> list = [key2, key3, key1]

Is this what you want?

dic = {'key1':'value1', 'key2':'value2', 'key3':'value3'}
keys = ['key2', 'key3', 'key1']
items = [dic[k] for k in keys]
print items

 Tim Chase 05-26-2006 05:42 PM

Re: sort a dictionary by keys in specific order

> hi i have a normal dictionary with key and value pairs. now i wanna
> sort by the keys BUT in a specific order i determine in a list !? any
> ideas
>
> dic = {'key1':'value1', 'key2':'value2', 'key3':'value3'}
>
> list = [key2, key3, key1]

can happen.

2) I presume your list is a list of strings, not of references:

order = ['key2', 'key3', 'key1']

3) As a dictionary is an unordered collection, I presume you want
a resulting list of key/value pairs.

If both #2 and #3 hold, you can use

results = [(k,dic[k]) for k in order]

which will return a list of tuples in the desired key order.

-tkc

 spohle 05-26-2006 05:50 PM

Re: sort a dictionary by keys in specific order

how do i get the result back into the dictionary ?

 Tim Chase 05-26-2006 06:08 PM

Re: sort a dictionary by keys in specific order

> how do i get the result back into the dictionary ?

Well, if you must, if you've just got the results in my
previous post, you can take them and shove them back into a
dict with

results = [('key1','value1'),('key2','value2)]
newDict = dict(results)

If you're not doing anything with that the resulting list of
ordered key/value pairs (such as inserting, printing,
whatever), then skip the whole matter:

newDict = dic

:)

HOWEVER...as I noted, a dictionary is an INHERENTLY UNSORTED
COLLECTION. There are some wrapper-classes around that will
feign sortedness if you want them, but they may not handle

There are few reasons for a sorted dict. Most of them
regard displaying them. If this is the case, just do the
sorting/selection of the items before you print:

print "\n".join([dic[k] for k in order])

Other reasons might involve dependancies, where some process
requires that you access the keys in a particular order.
Just order them before you call the process.

-tkc

 spohle 05-26-2006 06:26 PM

Re: sort a dictionary by keys in specific order

i write the dict out to a file, not with file methods but rather with
an inhouse python code. unfortunatly the order plays a big role for
that.

 Bruno Desthuilliers 05-26-2006 11:00 PM

Re: sort a dictionary by keys in specific order

Tim Chase a écrit :
(snip)
>
>> list = [key2, key3, key1]

>

s/command/type/

> funky stuff can happen.

indeed, if you shadow it with a non-compatible object !-)

 Bruno Desthuilliers 05-26-2006 11:03 PM

Re: sort a dictionary by keys in specific order

spohle a écrit :
> how do i get the result back into the dictionary ?
>

Python dicts (like almost any known hash-table) are *not* ordered. If
you need an ordered dict, roll your own - this is quite easy.

 Wolfgang Grafen 05-29-2006 09:49 AM

Re: sort a dictionary by keys in specific order

spohle wrote:
> hi i have a normal dictionary with key and value pairs. now i wanna
> sort by the keys BUT in a specific order i determine in a list !? any
> ideas
>
> dic = {'key1':'value1', 'key2':'value2', 'key3':'value3'}
>
> list = [key2, key3, key1]
>

You could use the seqdict package at
http://home.arcor.de/wolfgang.grafen...s/Modules.html

Regards

Wolfgang

 All times are GMT. The time now is 01:08 PM.