I wrote this code to print out the dictionary items that are sorted in alphabetical order of the 'Keys' but i want to sort the 'Items' according to increasing/decreasing order of the numerical values of 'Dictionary Values'
Note: The result must contain item tuples of the form (key, Values) and the sorted order should be according to increasing/decreasing order of numerical 'Values'
def calculate(sentence):
sentence = sentence.lower()
d = {}
for characters in sentence:
d[characters] = d.get(characters, 0) + 1
return sorted(d.items())
print(calculate("The quick brown fox jumps over the lazy dog"))
How can this be done? Please help me with this and if the solution is complex try explaining it as simple as possible. I'm only a beginner
For example: input = 'zzzzoopq' output = [('z', 4), ('o', 2), ('p', 1), ('q', 1)] (The output is sorted according to descending order of 'values')