编程 - Python 处理文件的性能优化
巴扎黑
巴扎黑 2017-04-17 13:15:38
[Python讨论组]

现在有一个list包含有1500个topic,另外一个文件包含一亿个微博数据,现在我想统计,1500个topic中每个topic分别有多少条微博包含它们,我写的代码如下,但是运行起来需要非常久的时间,有什么办法可以优化吗?

    f = file("largefile")
    for line in f:
        try:
            tweet_time = line.split(',',3)[2].split()[0]  # 微博发布时间
            tweet = line.split(',',3)[-1]  # 微博内容
            for topic in topics:
                topic_items = topic.split()  # 每个topic可能有多个词组成
                isContain = True
                for item in topic_items:
                    if item not in tweet:
                        isContain = False
                        break
                if isContain:
                    pass   # 该微博包含该topic
        except:
            continue
    f.close()
巴扎黑
巴扎黑

全部回复(1)
PHP中文网
  1. 分块
  2. 并行处理
  3. 加总

参见:

  1. http://docs.oracle.com/javase/tutorial/essential/concurrency/forkjoin.html
  2. http://en.wikipedia.org/wiki/MapReduce
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号