V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
gavinlw
V2EX  ›  Python

如何提高python中字典类型中的数据之间比较的效率

  •  
  •   gavinlw · 2012-11-09 21:44:47 +08:00 · 3794 次点击
    这是一个创建于 4185 天前的主题,其中的信息可能已经有所发展或是发生改变。
    有这样一个场景:
    一个字典中有1000个键值对,每个值是包含200个以内词语的列表
    如何快速计算出所有值(也就是说所有词语)在字典的某条记录中是否出现,以及出现次数~
    也就是统计记录中包含该词的条目数。
    2 条回复    1970-01-01 08:00:00 +08:00
    phuslu
        1
    phuslu  
       2012-11-09 21:48:19 +08:00
    collections.defaultdict(collections.Counter)

    python 2.7
    013231
        2
    013231  
       2012-11-09 22:46:02 +08:00
    這種問題用reduce解決最簡單了:
    http://gist.github.com/4046085
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   5324 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 29ms · UTC 07:16 · PVG 15:16 · LAX 00:16 · JFK 03:16
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.