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 6226
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/moduleheapq.html
Why are you doing that with keyvalue pairs? Why not with the array
module or lists?
Jeff <je******@gmail.comwrote:
Why are you doing that with keyvalue pairs? Why not with the array
module or lists?
The original poster asked about a problem with keyvalue pairs. I just
answered his question.
On Nov 12, 8:54 pm, Jeff <jeffo...@gmail.comwrote:
Why are you doing that with keyvalue 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 discussion thread is closed Replies have been disabled for this discussion. Similar topics
1 post
views
Thread by Gerrit Holl 
last post: by

90 posts
views
Thread by Christoph Zwerschke 
last post: by

7 posts
views
Thread by ProvoWallis 
last post: by

19 posts
views
Thread by ramu 
last post: by

14 posts
views
Thread by vatamane 
last post: by
  
13 posts
views
Thread by Nader 
last post: by

12 posts
views
Thread by Florian Brucker 
last post: by
          