![]() |
How to proceed - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: How to proceed (/thread-7587.html) |
How to proceed - jarrod0987 - Jan-16-2018 If I have a simple Dictionary. Lets say the Key is a char like A, B, C...etc and each key has a simple numerical value like 4 or 10 etc... What I want to get out is a sort of final report that tells me the numerical value of each key and also I want to print that list in order of highest number to lowest number with the key that it went with. Not sure what to do...How to go about it. Looking for a simple way. Thanks RE: How to proceed - Larz60+ - Jan-17-2018 Please post code RE: How to proceed - jarrod0987 - Jan-17-2018 I can't post the code because I don't know how to write it. That's what I'm asking. How can I print out the contents of a dictionary organized by value? RE: How to proceed - DeaD_EyE - Jan-17-2018 import string import operator import collections # example data chars = {c: n for n, c in enumerate(string.ascii_uppercase)} sorted_chars = sorted(chars.items(), key=operator.itemgetter(1)) # chars.items() returns a list with tuples # each tuple is a key-value pair # the sorted function accepts a key function for sorting # operator.itemgetter(1) returns a function, which returns # the second element of a list # if you want to have a "sorted" dict, you should use OrderedDict from collections sorted_chars_dict = collections.OrderedDict(sorted_chars) RE: How to proceed - jarrod0987 - Jan-17-2018 This is kind of above my head but I will try to read up on these commands during the weekend. Thank You. |