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


Groups > comp.lang.python > #10756 > unrolled thread

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

Started byThomas Jollans <t@jollybox.de>
First post2011-08-02 20:12 +0200
Last post2011-08-02 20:12 +0200
Articles 1 — 1 participant

Back to article view | Back to comp.lang.python

This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by below is the oldest one visible, not the original post.


Contents

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

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

FromThomas Jollans <t@jollybox.de>
Date2011-08-02 20:12 +0200
SubjectRe: how to sort a hash list without generating a new object?
Message-ID<mailman.1798.1312308758.1164.python-list@python.org>
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

[toc] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web