Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #10756

Re: how to sort a hash list without generating a new object?

Date 2011-08-02 20:12 +0200
From Thomas Jollans <t@jollybox.de>
Subject Re: how to sort a hash list without generating a new object?
References <CAN1Fwxd7_YPES14ZShOZswwkBdwnSoxK33YFXWdsU943oSztTw@mail.gmail.com>
Newsgroups comp.lang.python
Message-ID <mailman.1798.1312308758.1164.python-list@python.org> (permalink)

Show all headers | View raw


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

Back to comp.lang.python | Previous | Next | Find similar | Unroll thread


Thread

Re: how to sort a hash list without generating a new object? Thomas Jollans <t@jollybox.de> - 2011-08-02 20:12 +0200

csiph-web