By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,172 Members | 727 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,172 IT Pros & Developers. It's quick & easy.

Define key in nlargest() of heapq?

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

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
Share this Question
Share on Google+
1 Reply


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