Show me the code_0006题

作者: bluescorpio | 来源:发表于2016-05-11 17:27 被阅读144次

0006题:你有一个目录,放了你一个月的日记,都是txt,为了避免分词的问题,假设内容都是英文,请统计出你认为每篇日记最重要的词。

解题思路:可以用刚写的另一篇文章collections库里面的一些方法,比如Counter()most_common()
代码如下:

#! /usr/bin/env python
#coding=utf-8
import os
import re
from collections import Counter

def get_filepaths(directory):
    file_paths = []
    for root, directories, files in os.walk(directory):
        for filename in files:
            filepath = os.path.join(root, filename)
            file_paths.append(filepath)
            
    return file_paths

def counter_more_words(li):
    word_dict = Counter(li)
    return [i[0] for i in word_dict.most_common()[:10]]

if __name__ == '__main__':
    for file in get_filepaths(r'C:\diaries'):
        with open(file, 'r') as f:
            word_li = re.findall("\w+", f.read())
            print " ".join(counter_more_words(word_li))

相关文章

  • Show me the code_0006题

    第0006题:你有一个目录,放了你一个月的日记,都是txt,为了避免分词的问题,假设内容都是英文,请统计出你认为每...

  • Show me the code_0001题

    第0001题:做为Apple Store App独立开发者,你要搞限时促销,为你的应用生成激活码(或者优惠券),使...

  • Show me the code_0002题

    第0002题:将0001题生成的200个激活码(或者优惠券)保存到MySQL关系型数据库中。如果第0001题顺利解...

  • 夜未眠,梦将醒

    If you want me, show me If you need me, tell me ...

  • 岁月静好,现世安稳

    Show me what love is-haven’t got a clue. Show me what lov...

  • github/Show me the code (0)

    https://github.com/Show-Me-the-Code/show-me-the-code第 000...

  • A little love

    Greatness as you Smallest as me You show me what is deep ...

  • 7.30

    show me the money 4里你遗憾离开,show me the money 6中你强势归来.抱着诸多感...

  • Java干货 - 遍历Map的四种方法

    SHOW ME THE FOOLISH CODE

  • sweet love

    show me your face and let me hear your voice,for your ...

网友评论

    本文标题:Show me the code_0006题

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