473,325 Members | 2,442 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,325 software developers and data experts.

How to get a set of keys with largest values?

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

Nov 12 '07 #1
6 6356
On Nov 12, 10:07 am, Davy <zhushe...@gmail.comwrote:
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

Nov 12 '07 #2
Why are you doing that with key-value pairs? Why not with the array
module or lists?

Nov 12 '07 #3
Jeff <je******@gmail.comwrote:
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.
Nov 12 '07 #4
On Nov 12, 8:54 pm, Jeff <jeffo...@gmail.comwrote:
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

Nov 13 '07 #5
On Nov 12, 1:07 am, Davy <zhushe...@gmail.comwrote:
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.

Nov 13 '07 #6
On Nov 12, 1:07 am, Davy <zhushe...@gmail.comwrote:
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:]]

Nov 13 '07 #7

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

Similar topics

1
by: Gerrit Holl | last post by:
Hi, is it guaranteed that dict(zip(d.keys(), d.values())) == d? In words, do .keys() and .values() always have the same order? Is it safe to rely on this? yours, Gerrit. --
90
by: Christoph Zwerschke | last post by:
Ok, the answer is easy: For historical reasons - built-in sets exist only since Python 2.4. Anyway, I was thinking about whether it would be possible and desirable to change the old behavior in...
7
by: ProvoWallis | last post by:
I'm still learning python so this might be a crazy question but I thought I would ask anyway. Can anyone tell me if it is possible to join two dictionaries together to create a new dictionary using...
19
by: ramu | last post by:
Hi, I have, suppose 1000 numbers, in a file. I have to find out 5 largest numbers among them without sorting. Can you please give me an efficient idea to do this? My idea is to put those numbers...
14
by: vatamane | last post by:
This has been bothering me for a while. Just want to find out if it just me or perhaps others have thought of this too: Why shouldn't the keyset of a dictionary be represented as a set instead of a...
3
by: HEMH6 | last post by:
Who can help solve this problem??? Finding the Largest Value (a) write a function, largest(), that returns the largest value in a signed integer array. The array and its size are passed as...
4
by: sonia.sardana | last post by:
I know how how to retrive the largest & second largest salary. tell me how to retrive the 3,4,5,...............Largest salary. Create table empl(empid int, empname varchar,salary int) insert...
13
by: Nader | last post by:
Hello, I have a dictionary and will get all keys which have the same values. d = {('a' : 1), ('b' : 3), ('c' : 2),('d' : 3),('e' : 1),('f' : 4)} I will something as : d.keys(where their...
12
by: Florian Brucker | last post by:
Hi everybody! Given a dictionary, I want to create a clustered version of it, collecting keys that have the same value: {1:, 2:, 3:} That is, generate a new dict which holds for each value...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
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...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

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.