Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Need your small help
#1
Dear ALL,

How can I retrieve top 100 keys from the dictionary base on value?

that should return key and its value....

please help me Pray Pray Pray Pray
Reply
#2
what
have
you
tried?
Reply
#3
As well as provide a sample of the dictionary and explain more fully what you mean by "top 100 keys".  As buran asks, what code have you written so far?  What is the outcome you expect and what is the outcome you actually get, as well as any errors.  Make sure you use the proper code tags when posting.  If you are unsure how to do that, consult the Help document.
If it ain't broke, I just haven't gotten to it yet.
OS: Windows 10, openSuse 42.3, freeBSD 11, Raspian "Stretch"
Python 3.6.5, IDE: PyCharm 2018 Community Edition
Reply
#4
Make a reverse dictionary and sort the keys.
Unless noted otherwise, code in my posts should be understood as "coding suggestions", and its use may require more neurones than the two necessary for Ctrl-C/Ctrl-V.
Your one-stop place for all your GIMP needs: gimp-forum.net
Reply
#5
(Mar-16-2017, 01:03 PM)Ofnuts Wrote: Make a reverse dictionary and sort the keys.

Not a good idea - values may not be unique thus loosing some key-value pairs from original dict. OrderdDict docs (examples and recepies section) offers an example, but given we are in homework section it's not clear if use of OrderedDict is allowed
Reply
#6
Quote:in homework section it's not clear if use of OrderedDict is allowed
Or if smart use 36 where dict are ordered.
# Python 34
>>> d = {'car': 1, 'cab': 2, 'bus': 3}
>>> d
{'bus': 3, 'cab': 2, 'car': 1}

# Python 36
>>> d = {'car': 1, 'cab': 2, 'bus': 3}
>>> d
{'car': 1, 'cab': 2, 'bus': 3} 
Reply
#7
(Mar-16-2017, 02:22 PM)snippsat Wrote: Or if smart use 36 where dict are ordered.

however one should keep in mind that
Quote:The order-preserving aspect of this new implementation is considered an implementation detail and should not be relied upon (this may change in the future, but it is desired to have this new dict implementation in the language for a few releases before changing the language spec to mandate order-preserving semantics for all current and future Python implementations;
https://docs.python.org/3.6/whatsnew/3.6...ompactdict
Reply
#8
Show us what you've tried. We're not going to write the code for you :)

But there are several ways to do it (some more efficient than others), but since this is homework, speed/memory usage probably aren't your concern. So think about it, try a couple things, and show us some code, and we'll help direct you to a working solution.
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020