根据值对python中的元组进行排序 [英] Sorting tuples in python based on their values

查看:295
本文介绍了根据值对python中的元组进行排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用以下代码来打印前10个常见单词.但是,它不起作用.关于如何解决它的任何想法?

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

推荐答案

tmp = sorted(word_count_pairs, key=lambda pair: pair[0], reverse=True)[0:10]

说明:

  • sorted()key参数允许您在比较之前在每个元素上运行一个函数.
  • lambda pair: pair[0]是一个从word_count_pairs中提取数字的函数.
  • reverse以降序而不是升序排序.
  • The key parameter of sorted() allows you to run a function on each element before comparison.
  • lambda pair: pair[0] is a function that extracts the number from your word_count_pairs.
  • reverse sorts in descending order, instead of ascending order.

来源:

  • https://wiki.python.org/moin/HowTo/Sorting#Key_Functions
  • https://docs.python.org/2/library/functions.html#sorted

放在一边:如果您有很多不同的单词,则对整个列表进行排序以找到前十名是无效的.有更有效的算法.另一个答案中提到的most_common()方法可能利用了更有效的算法.

aside: If you have many different words, sorting the entire list to find the top ten is inefficient. There are much more efficient algorithms. The most_common() method mentioned in another answers probably utilizes a more efficient algorithm.

这篇关于根据值对python中的元组进行排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆