Sort Counter by frequency, then alphabetically in Python

后端 未结 4 992
被撕碎了的回忆
被撕碎了的回忆 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:14

    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']
    

提交回复
热议问题