Re: sort a dictionary by keys in specific order

2006-05-29 Thread Wolfgang Grafen
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

Re: sort a dictionary by keys in specific order

2006-05-26 Thread Bruno Desthuilliers
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. -- http://mail.python.org/mailman/listinfo/python-list

Re: sort a dictionary by keys in specific order

2006-05-26 Thread Bruno Desthuilliers
Tim Chase a écrit : (snip) > >> list = [key2, key3, key1] > > 1) it's bad practice to shadow the list() command... s/command/type/ > funky stuff can happen. indeed, if you shadow it with a non-compatible object !-) -- http://mail.python.org/mailman/listinfo/python-list

Re: sort a dictionary by keys in specific order

2006-05-26 Thread spohle
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. -- http://mail.python.org/mailman/listinfo/python-list

Re: sort a dictionary by keys in specific order

2006-05-26 Thread Tim Chase
> 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

Re: sort a dictionary by keys in specific order

2006-05-26 Thread spohle
how do i get the result back into the dictionary ? -- http://mail.python.org/mailman/listinfo/python-list

Re: sort a dictionary by keys in specific order

2006-05-26 Thread Paul Rubin
"spohle" <[EMAIL PROTECTED]> 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 ite

Re: sort a dictionary by keys in specific order

2006-05-26 Thread Tim Chase
> 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] 1) it's bad practice to shadow the list() command...f

sort a dictionary by keys in specific order

2006-05-26 Thread spohle
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] -- http://mail.python.org/mailman/listinfo/python-list