How to get a set of keys with largest values?

D

Davy

Hi all,

I have a dictionary with n elements, and I want to get the m(m<=n)
keys with the largest values.

For example, I have dic that includes n=4 elements, I want m=2 keys
have the largest values)
dic = {0:4,3:1,5:2,7:8}
So, the the largest values are [8,4], so the keys are [7,0].

Is there any fast way to implement this algorithm?
Any suggestions are welcome!

Best regards,
Davy
 
C

cokofreedom

Hi all,

I have a dictionary with n elements, and I want to get the m(m<=n)
keys with the largest values.

For example, I have dic that includes n=4 elements, I want m=2 keys
have the largest values)
dic = {0:4,3:1,5:2,7:8}
So, the the largest values are [8,4], so the keys are [7,0].

Is there any fast way to implement this algorithm?
Any suggestions are welcome!

Best regards,
Davy

Have a look at http://docs.python.org//lib/module-heapq.html
 
D

Duncan Booth

Davy said:
For example, I have dic that includes n=4 elements, I want m=2 keys
have the largest values)
dic = {0:4,3:1,5:2,7:8}
So, the the largest values are [8,4], so the keys are [7,0].

Is there any fast way to implement this algorithm?
Any suggestions are welcome!

Don't implement it: just use it.
dic = {0:4,3:1,5:2,7:8}
from heapq import nlargest
nlargest(2, dic, key=dic.__getitem__) [7, 0]
 
D

Duncan Booth

Jeff said:
Why are you doing that with key-value pairs? Why not with the array
module or lists?
The original poster asked about a problem with key-value pairs. I just
answered his question.
 
D

Davy

Why are you doing that with key-value pairs? Why not with the array
module or lists?

Hi,

The original question is a bit complex. I have to implement a sparse
matrix which use a dict of dict implementation.
For example, sp_mat = {1:{2:1,3:4},2:{4:6,8:9}}.

And I want to get m key with the largest values in each row? Any good
ideas?

Best regards,
Davy
 
D

DouhetSukd

Hi all,

I have a dictionary with n elements, and I want to get the m(m<=n)
keys with the largest values.

For example, I have dic that includes n=4 elements, I want m=2 keys
have the largest values)
dic = {0:4,3:1,5:2,7:8}
So, the the largest values are [8,4], so the keys are [7,0].

Is there any fast way to implement this algorithm?
Any suggestions are welcome!

Best regards,
Davy

#get a list of tuples, with value in 1st position, key second
li = [(value,key) for key, value in dic.items()]

#sort the list
li.sort()

m = 2
#grab the m highest values, from the end of the list
li_high_keys = [k for v, k in li[-m:]]

p.s. sorry if this ends up appearing multiple times, google groups can
be a bit cranky at times with postings.
 
D

DouhetSukd

Hi all,

I have a dictionary with n elements, and I want to get the m(m<=n)
keys with the largest values.

For example, I have dic that includes n=4 elements, I want m=2 keys
have the largest values)
dic = {0:4,3:1,5:2,7:8}
So, the the largest values are [8,4], so the keys are [7,0].

Is there any fast way to implement this algorithm?
Any suggestions are welcome!

Best regards,
Davy

#get a list of tuples, with value in 1st position, key second
li = [(value,key) for key, value in dic.items()]

#sort the list
li.sort()

m = 2
#grab the m highest values, from the end of the list
li_high_keys = [k for v, k in li[-m:]]
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,540
Members
45,025
Latest member
KetoRushACVFitness

Latest Threads

Top