美文网首页
python 词频统计

python 词频统计

作者: 旺旺碎彬彬 | 来源:发表于2018-10-07 12:08 被阅读0次

    """Count words."""

    def count_words(s, n):

        """Return the n most frequently occuring words in s."""

        from operator import itemgetter, attrgetter 

        # TODO: Count the number of occurences of each word in s

        strl_ist = s.replace('\n', ' ').lower().split(' ')

        count_dict = {}

        for str in strl_ist:

            if str in count_dict.keys():

                count_dict[str] = count_dict[str] + 1

            else:

                count_dict[str] = 1

        # TODO: Sort the occurences in descending order (alphabetically in case of ties)

        count_list=sorted(count_dict.iteritems(),key=itemgetter(1),reverse=True)

        top_n = count_list[0:n]

        # TODO: Return the top n most frequent words.

        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()

    相关文章

      网友评论

          本文标题:python 词频统计

          本文链接:https://www.haomeiwen.com/subject/tqklaftx.html