Sort Counter by frequency, then alphabetically in Python

后端 未结 4 2018
日久生厌
日久生厌 2020-12-17 02:25

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:16

    For the sake of completeness, to get the single-occurrence letters in alphabetical order:

    letter_count = collections.Counter("alphabet")
    
    single_occurrences = sorted([letter for letter, occurrence in letter_count.items() if occurrence == 1])
    print(single_occurrences)
    # prints: ['b', 'e', 'h', 'l', 'p', 't']
    

提交回复
热议问题