设为首页收藏本站
开启辅助访问
切换到窄版

 找回密码
 注册

QQ登录

只需一步,快速开始

搜索
查看: 1520|回复: 7

python统计字符串中不同单词的数量

[复制链接]
发表于 2013-8-13 09:23:09 | 显示全部楼层 |阅读模式
text = "ga bu zo meuh ga zo bu meuh meuh ga zo zo meuh zo bu zo"
items = text.split(' ')

counters = {}
for item in items:
    if item in counters:
        counters[item] += 1
    else:
        counters[item] = 1

print "Count of different word:"
print counters

print "Most popular word:"
print sorted([(counter,word) for word,counter in counters.items()],reverse=True)[0][1]

#显示结果:

Count of different word:
{'bu': 3, 'zo': 6, 'meuh': 4, 'ga': 3}
Most popular word:
zo
#该代码片段来自于: http://www.sharejs.com/codes/python/1871
您需要登录后才可以回帖 登录 | 注册

本版积分规则

QQ|申请友链|小黑屋|手机版|Archiver|生物信息学论坛 ( 蜀ICP备09031721号  

GMT+8, 2017-2-23 15:24 , Processed in 0.096769 second(s), 21 queries .

Powered by Discuz! X3

© 2001-2013 Comsenz Inc.

快速回复 返回顶部 返回列表