471,330 Members | 1,782 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,330 software developers and data experts.

Define key in nlargest() of heapq?

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].

How to do this by nlargest() of heapq? I have tried
nlargest(2,dic,key),
the interpreter give out:
Traceback (most recent call last):
File "<interactive input>", line 1, in <module>
NameError: name 'key' is not defined

Best regards,
Davy

Nov 13 '07 #1
1 3093
Davy <zh*******@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].

How to do this by nlargest() of heapq? I have tried
nlargest(2,dic,key),
the interpreter give out:
Traceback (most recent call last):
File "<interactive input>", line 1, in <module>
NameError: name 'key' is not defined

Best regards,
Davy
You could use the code I posted for you yesterday. See message

http://groups.google.co.uk/group/com...05259c41c78fa0

Nov 13 '07 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

reply views Thread by Eric | last post: by
reply views Thread by Stefan Behnel | last post: by
18 posts views Thread by Bryan Parkoff | last post: by
3 posts views Thread by theotyflos | last post: by
18 posts views Thread by bearophileHUGS | last post: by
2 posts views Thread by jm.suresh | last post: by
4 posts views Thread by George Sakkis | last post: by
7 posts views Thread by Giampaolo Rodola' | last post: by

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.