My problem has probably quite easy solution but I'm a Python beginner and can't do that.
b = input()
a = b.split()
from collections import Counter
myDict = Counter(a)
import operator
test_dict = myDict
wynik = sorted(test_dict.items(), key=operator.itemgetter(1))
print(wynik)
Why wynik
isn't sorted?
Your data is sorted. In ascending order by value. If you wanted to have the counts in descending order, use reverse=True
to reverse the sorted order:
sorted(test_dict.items(), key=operator.itemgetter(1), reverse=True)
Note that you don't need to sort yourself; use the Counter.most_common()
method instead:
wynik = test_dict.most_common()
This method returns the keys and counts in descending order already:
>>> from collections import Counter
>>> counts = Counter('abc abc qwerty abc bla bla bla abc'.split())
>>> counts.most_common()
[('abc', 4), ('bla', 3), ('qwerty', 1)]
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.