Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sorting tuples in python based on their values [duplicate]

I am trying to print the top 10 frequent words using the following code. However, its not working. Any idea on how to fix it?

def reducer_count_words(self, word, counts):
    # send all (num_occurrences, word) pairs to the same reducer.
    # num_occurrences is so we can easily use Python's max() function.
    yield None, (sum(counts), word)




# discard the key; it is just None
def reducer_find_max_10_words(self, _, word_count_pairs):
    # each item of word_count_pairs is (count, word),
    # so yielding one results in key=counts, value=word

        tmp = sorted(word_count_pairs)[0:10]
        yield tmp
like image 965
A.M. Avatar asked Jul 13 '26 14:07

A.M.


1 Answers

Use collections.Counter and its most_common method:

>>>from collections import Counter
>>>my_words = 'a a foo bar foo'
>>>Counter(my_words.split()).most_common()
[('foo', 2), ('a', 2), ('b', 1)]
like image 186
BeetDemGuise Avatar answered Jul 16 '26 04:07

BeetDemGuise



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!