Sort Counter by frequency, then alphabetically in Python

后端 未结 4 997
被撕碎了的回忆
被撕碎了的回忆 2020-12-17 02:51

I am trying to use counter to sort letters by occurrence, and put any that have the same frequency into alphabetical order, but I can\'t get access to the Value of the dicti

4条回答
  •  误落风尘
    2020-12-17 03:21

    You can sort the input before passing it to the counter.

    >>> Counter(sorted("alphabet")).most_common()
    [('a', 2), ('b', 1), ('e', 1), ('h', 1), ('l', 1), ('p', 1), ('t', 1)]
    

提交回复
热议问题