3 ms·
import operator str = "abcaaabcccaabrrcb" dict = {ch: str.count(ch) for ch in str} sorted_items = sorted(dict.items(), key=operator.itemge
by koenigdavidmj 16y ago
import operator
str = "abcaaabcccaabrrcb"
dict = {ch: str.count(ch) for ch in str}
sorted_items = sorted(dict.items(), key=operator.itemgetter(1), reverse=True)
The dictionary comprehension syntax is new in Python 2.7 and 3k, but you can say
dict((ch, str.count(ch)) for ch in str)
instead.