how to sort a hash list without generating a new object?
Thomas Jollans
t at jollybox.de
Tue Aug 2 14:12:49 EDT 2011
On 02/08/11 20:02, smith jack wrote:
> the source code is as follows
>
> x={}
> x['a'] = 11
> x['c'] = 19
> x['b'] = 13
> print x
>
> tmp = sorted(x.items(), key = lambda x:x[0]) # increase order by
> default, if i want to have a descending order, what should i do?
> # after sorted is called, a list will be generated, and the hash list
> x is not changed at all, how to convert x to a sorted hash list
> without generating a new object?
> print tmp
> print x
Python dictionaries are never ordered. Perhaps the
collections.OrderedDict class can do what you're looking for.
http://docs.python.org/py3k/library/collections.html#collections.OrderedDict
More information about the Python-list
mailing list