I have created a dictionary using dict()
where the key is a word and the item is the amount of time that word occurs in a text file.
I am trying to sort on the amount of occurrences a word has from high to low.
An example of the input is:
{'stack': 2, 'over': 1, 'flow': 3}
The desired output is any type of list that shows both the word and its occurrences i.e.:
[['flow', 3], ['stack', 2], ['over', 1]]
I have tried using:
sorted(word_dict, key=word_dict.get, reverse=True)
output:
['flow', 'stack', 'over']
which I found on other SO posts; however, this only outputs the words and not the occurrences, how could I solve this?
CodePudding user response:
You can iterate over the sorted keys and construct the new list:
lst = [[k, dct[k]] for k in sorted(dct, key=dct.get, reverse=True)]
print(lst)
Prints:
[['flow', 3], ['stack', 2], ['over', 1]]
Or: sort the dictionary items directly:
lst = sorted(map(list, dct.items()), key=lambda x: x[1], reverse=True)
print(lst)
CodePudding user response:
If you want to conserve all the information from a dictionary when sorting it, the typical first step is to call the .items() method on the dictionary. Calling .items() on the dictionary will provide an iterable of tuples representing the key-value pairs:
Crucially, you can use the sorted() function with dictionary and call the .items() method and use the result as an argument to the sorted() function. Using .items() keeps all the information from the dictionary:
for eg:->
people = {3: "Jim", 2: "Jack", 4: "Jane", 1: "Jill"}
# Sort key
def value_getter(item):
return item[1]
sorted(people.items(), key=value_getter)
#ans:-> [(2, 'Jack'), (4, 'Jane'), (1, 'Jill'), (3, 'Jim')]