码迷,mamicode.com
首页 > 其他好文 > 详细

udacity 机器学习课程 统计字数

时间:2016-07-08 23:05:19      阅读:204      评论:0      收藏:0      [点我收藏+]

标签:

"""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
    word = s.split()
    worddict = {}
    for item in word:
        if item not in worddict:
            worddict[item] = 1
        else:
            worddict[item] += 1
    
    # TODO: Sort the occurences in descending order (alphabetically in case of ties) 
    sortedWorddict = sorted(worddict.iteritems(), key = lambda x:(-x[1],x[0]),     reverse = False)

    # TODO: Return the top n words as a list of tuples (<word>, <count>)
    top_n = []
    for i in range(n):
        top_n.append(sortedWorddict[i])
    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()

 

udacity 机器学习课程 统计字数

标签:

原文地址:http://www.cnblogs.com/lixiang-/p/5654793.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!