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 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
Why are you doing that with key-value pairs? Why not with the array
module or lists?
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.
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
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.
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:]] This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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.
--
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
by: ryjfgjl |
last post by:
ExcelToDatabase: batch import excel into database automatically...
|
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: 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)...
|
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...
|
by: Defcon1945 |
last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
|
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....
|
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
| |