Finding the most popular words in a list

前端 未结 3 1032
我寻月下人不归
我寻月下人不归 2021-02-05 22:32

I have a list of words:

words = [\'all\', \'awesome\', \'all\', \'yeah\', \'bye\', \'all\', \'yeah\']

And I want to get a list of tuples:

相关标签:
3条回答
  • 2021-02-05 23:00

    Is it Pythonic, elegant and efficient?

    Looks good to me...

    Are you able to do it better?

    "better"? If it's understandable, and efficient, isn't that enough?

    Maybe look at defaultdict to use that instead of setdefault.

    0 讨论(0)
  • 2021-02-05 23:22

    The defaultdict collection is what you are looking for:

    from collections import defaultdict
    
    D = defaultdict(int)
    for word in words:
        D[word] += 1
    

    That gives you a dict where keys are words and values are frequencies. To get to your (frequency, word) tuples:

    tuples = [(freq, word) for word,freq in D.iteritems()]
    

    If using Python 2.7+/3.1+, you can do the first step with a builtin Counter class:

    from collections import Counter
    D = Counter(words)
    
    0 讨论(0)
  • 2021-02-05 23:23

    You can use the counter for this.

    import collections
    words = ['all', 'awesome', 'all', 'yeah', 'bye', 'all', 'yeah']
    counter = collections.Counter(words)
    print(counter.most_common())
    >>> [('all', 3), ('yeah', 2), ('bye', 1), ('awesome', 1)]
    

    It gives the tuple with reversed columns.

    From the comments: collections.counter is >=2.7,3.1. You can use the counter recipe for lower versions.

    0 讨论(0)
提交回复
热议问题