udacity 机器学习课程 统计字数

"""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()
原文地址:https://www.cnblogs.com/lixiang-/p/5654793.html