Sorting tuples in python based on their values
本问题已经有最佳答案,请猛点这里访问。
我正在尝试使用以下代码打印前10个常见单词。但是,它不起作用。你知道怎么修吗?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | 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 |
使用
1 2 3 4 | >>>from collections import Counter >>>my_words = 'a a foo bar foo' >>>Counter(my_words.split()).most_common() [('foo', 2), ('a', 2), ('b', 1)] |
号
使用collections.most-common()。
例子:
1 2 3 4 5 6 | most_common([n]) Return a list of the n most common elements and their counts from the most common to the least. If n is not specified, most_common() returns all elements in the counter. Elements with equal counts are ordered arbitrarily: >>> from collections import Counter >>> Counter('abracadabra').most_common(3) [('a', 5), ('r', 2), ('b', 2)] |
1 | tmp = sorted(word_count_pairs, key=lambda pair: pair[0], reverse=True)[0:10] |
说明:
sorted() 的key 参数允许您在比较之前对每个元素运行一个函数。lambda pair: pair[0] 是一个从单词"count"对中提取数字的函数。reverse 按降序排序,而不是按升序排序。
资料来源:
- https://wiki.python.org/moin/howto/sorting key_函数
- https://docs.python.org/2/library/functions.html排序
旁白:如果你有许多不同的单词,那么对整个列表进行排序以找到前十个单词是没有效率的。有很多更有效的算法。另一个答案中提到的