Order by value in dictionary
cokofreedom at gmail.com
cokofreedom at gmail.com
Wed Oct 17 10:00:46 EDT 2007
On Oct 17, 3:39 pm, Abandoned <best... at gmail.com> wrote:
> Hi..
> I have a dictionary like these:
> a={'a': '1000', 'b': '18000', 'c':'40', 'd': '600'} ...... 100.000
> element
> I want to sort this by value and i want to first 100 element..
> Result must be:
> [b, a, d, c .....] ( first 100 element)
>
> I done this using FOR and ITERATOR but it tooks 1 second and this is
> very big time to my project.
> I want to learn the fastest method..
>
> I'm sorry my bad english.
> Please help me.
> King regards..
I take it you did something like this
>>> items = d.items()
>>> items = [(v, k) for (k, v) in items]
>>> items.sort()
>>> items.reverse() # so largest is first
>>> items = [(k, v) for (v, k) in items]
?
More information about the Python-list
mailing list