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.. 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]
?
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
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
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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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,...
|
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...
|
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...
|
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....
|
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...
|
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
|
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}
|
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...
|
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',...
|
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...
|
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...
|
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...
|
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: 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 =...
|
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...
|
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...
|
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...
|
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...
|
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...
| |