473,231 Members | 2,940 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,231 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 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..

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...
0
by: VivesProcSPL | last post by:
Obviously, one of the original purposes of SQL is to make data query processing easy. The language uses many English-like terms and syntax in an effort to make it easy to learn, particularly for...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 3 Jan 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). For other local times, please check World Time Buddy In...
0
by: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
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...

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.