473,287 Members | 1,418 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,287 software developers and data experts.

Order by value in dictionary

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..

Oct 17 '07 #1
4 1621
On Oct 17, 3:39 pm, Abandoned <best...@gmail.comwrote:
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]
?

Oct 17 '07 #2
Very very thanks everbody..

These are some method..
Now the fastest method is second..

==== 1 ===
def sortt(d):
items=d.items()
backitems=[ [v[1],v[0]] for v in items]
backitems.sort()
#boyut=len(backitems)
#backitems=backitems[boyut-500:]
a=[ backitems[i][1] for i in range(0,len(backitems))]
a.reverse()
return a

==== 2 =====
import operator
def sortt(d):
backitems=d.items()
boyut=len(backitems)
backitems=backitems[boyut-500:]
backitems=sorted(backitems, key=operator.itemgetter(1))
a=[ backitems[i][0] for i in range(0,len(backitems))]
a.reverse()
return a

==== 3 =====
def sortt(d):
backitems=d.items()
backitems.sort(lambda x,y:cmp(x[1],y[1]))
backitems=sorted(backitems, key=operator.itemgetter(1))
a=[ backitems[i][0] for i in range(0,len(backitems))]
a.reverse()
return a

====== 4 =======
import heapq

def sortt(d):
backitems=d.items()
backitems=heapq.nlargest(1000, backitems, operator.itemgetter(1))
a=[ backitems[i][0] for i in range(0,len(backitems))]
a.reverse()
return a

Oct 17 '07 #3
On Wed, 17 Oct 2007 08:09:50 -0700, Abandoned wrote:
Very very thanks everbody..

These are some method..
Now the fastest method is second..
Maybe because the second seems to be the only one that's not processing
the whole dictionary but just 500 items less!?

You are building way too much intermediate lists in your functions.
==== 1 ===
def sortt(d):
items=d.items()
Builds a list of all items.
backitems=[ [v[1],v[0]] for v in items]
Builds another list from all items.
backitems.sort()
a=[ backitems[i][1] for i in range(0,len(backitems))]
And again a new list *plus* a list of len(backitems) integers that is
built just to iterate over it. Instead of iterating directly over
`backitems` without the index.
a.reverse()
return a
This whole function can be written as (untested):

def sortt(d):
sorted_items = sorted((item[1], item[0]) for item in d.iteritems(),
reverse=True)
return map(operator.itemgetter(1), sorted_items)
==== 2 =====
import operator
def sortt(d):
backitems=d.items()
boyut=len(backitems)
backitems=backitems[boyut-500:]
backitems=sorted(backitems, key=operator.itemgetter(1))
a=[ backitems[i][0] for i in range(0,len(backitems))]
a.reverse()
return a
Without throwing away 500 items:

def sortt(d):
sorted_items = sorted(d.iteritems(),
key=operator.itemgetter(1),
reverse=True)
return map(operator.itemgetter(0), sorted_items)

Ciao,
Marc 'BlackJack' Rintsch
Oct 17 '07 #4
On 10/17/07, Abandoned <be*****@gmail.comwrote:
Very very thanks everbody..

These are some method..
Now the fastest method is second..

==== 1 ===
def sortt(d):
items=d.items()
backitems=[ [v[1],v[0]] for v in items]
backitems.sort()
#boyut=len(backitems)
#backitems=backitems[boyut-500:]
a=[ backitems[i][1] for i in range(0,len(backitems))]
a.reverse()
return a

==== 2 =====
import operator
def sortt(d):
backitems=d.items()
boyut=len(backitems)
backitems=backitems[boyut-500:]
backitems=sorted(backitems, key=operator.itemgetter(1))
a=[ backitems[i][0] for i in range(0,len(backitems))]
a.reverse()
return a

==== 3 =====
def sortt(d):
backitems=d.items()
backitems.sort(lambda x,y:cmp(x[1],y[1]))
backitems=sorted(backitems, key=operator.itemgetter(1))
a=[ backitems[i][0] for i in range(0,len(backitems))]
a.reverse()
return a

====== 4 =======
import heapq

def sortt(d):
backitems=d.items()
backitems=heapq.nlargest(1000, backitems, operator.itemgetter(1))
a=[ backitems[i][0] for i in range(0,len(backitems))]
a.reverse()
return a
Btw, there are specialized algorithms called "Selection Algorithms"
for finding k largest items in a collection.

http://en.wikipedia.org/wiki/Selection_algorithm

Cheers,
--
--
Amit Khemka
Oct 18 '07 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Brainwashed | last post by:
Is there any order in dictionaries that will never change ? I've noticed that assigning always the same elements to the dict puts them always in the same order. Like regexp which takes 3 values,...
0
by: Christos TZOTZIOY Georgiou | last post by:
Hi all, this post contains at the end a handy module that I've used quite often when I wanted to analyse the occasional complex expression and how it was to be evaluated. The function...
2
by: Tim Daneliuk | last post by:
I am aware that dictionary order is not guaranteed. But I ran into something puzzling today. I filled a dictionary dynamically as a program ran. Up to a certain point, the key order was the...
7
by: svilen | last post by:
hello again. i'm now into using python instead of another language(s) for describing structures of data, including names, structure, type-checks, conversions, value-validations, metadata etc....
39
by: Nicolas Fleury | last post by:
In the following example: class MyMetaclass(type): pass class MyBaseType(object): __metaclass__ = MyMetaclass class MyType(MyBaseType): x = 4 y = 5 z = 6 Is there any way to modify...
5
by: Rakesh | last post by:
Hi, For a particular problem of mine, I want to sort <key, value> pairs by its value. Eg: Input: A, 4 B, 5
7
by: Marcio Rosa da Silva | last post by:
Hi! In dictionaries, unlinke lists, it doesn't matter the order one inserts the contents, elements are stored using its own rules. Ex: >>> d = {3: 4, 1: 2} >>> d {1: 2, 3: 4}
2
by: orekinbck | last post by:
Hi There I am probably missing something fundamental here, but I cannot see a method to search the values of a generic dictionary so that I can find the key ? Of course I could enumerate...
8
by: spohle | last post by:
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',...
8
by: Brian L. Troutwine | last post by:
I've got a problem that I can't seem to get my head around and hoped somebody might help me out a bit: I've got a dictionary, A, that is arbitarily large and may contains ints, None and more...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.