473,231 Members | 2,940 Online

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

King regards..

Oct 17 '07 #1
4 1619
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..

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.