Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to proceed
#1
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
Reply
#2
Please post code
Reply
#3
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?
Reply
#4
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)
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply
#5
This is kind of above my head but I will try to read up on these commands during the weekend. Thank You.
Reply


Forum Jump:

User Panel Messages

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