|
|
5鱼币
本帖最后由 felix_du 于 2016-8-13 21:37 编辑
一个排序问题,取字符串中出现最多的n个单词,若出现次数一样,按字母表顺序往后排。
我的思路就是先将单词和出现的次数存入字典,然后通过排序,将出现次数最多的单词存入列表中。
现在的问题在于,我可以通过value排序后,无法保证出现同等次数的单词按照字母表顺序排列。求帮忙。
- """Count words."""
- def count_words(s, n):
- """Return the n most frequently occuring words in s."""
-
- # TODO: Count the number of occurences of each word in s
-
- # TODO: Sort the occurences in descending order (alphabetically in case of ties)
-
- # TODO: Return the top n words as a list of tuples (<word>, <count>)
- s_list = s.split()
- lenth = len(s_list)
- counters ={}
- for i in range(lenth):
- if s_list[i] in counters:
- counters[s_list[i]] += 1
- else:
- counters[s_list[i]] = 1
- counter_sort = sorted(counters.items(),key=lambda item:item[1],reverse =True)
- top_n_list = []
- for i in range(n):
- top_n_list.append((counter_sort[i][0],counter_sort[i][1]))
- top_n = tuple(top_n_list)
-
-
- return top_n
- def test_run():
- """Test count_words() with some inputs."""
- print count_words("cat bat mat cat bat cat", 3)
- print count_words("betty bought a bit of butter but the butter was bitter", 3)
- if __name__ == '__main__':
- test_run()
复制代码
|
|