P: n/a

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  
Share this Question
P: n/a

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  
P: n/a

Why are you doing that with keyvalue pairs? Why not with the array
module or lists?  
P: n/a

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.  
P: n/a

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  
P: n/a

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.  
P: n/a

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.   Question stats  viewed: 5864
 replies: 6
 date asked: Nov 12 '07
