Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

    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.


Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: