前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Python 的mapreduce 单词统计

Python 的mapreduce 单词统计

原创
作者头像
好派笔记
修改2021-11-03 12:43:46
6710
修改2021-11-03 12:43:46
举报
文章被收录于专栏:好派笔记好派笔记
代码语言:javascript
复制
#!/usr/bin/env python
import random
'abc..z'
alphaStr = "".join(map(chr, range(97,123)))
fp = open("word.txt", "w")
maxIter = 100000
for i in range(maxIter):
    word = ""
    len =random.randint(1,5)
    for j in range(len):
        word + = alphaStr[random.randint(0,25)]
        fp.write(word + '\n')
fp.close()
cat word.txt | ./wordcount_mapper.py | ./wordcount_reducer.py .
word count reduce,   python
filename:  wordcount_reducer.py
from  operator import itemgetter
import sys
wordcount = {}
for line in sys.stdin:
    word, count = line.strip().split('\t',1)
    try:
        count = int(count)
        wordcount[word] = wordcount.get(word,0) + count
    except ValueError
        pass
sorted_wordcount = sorted(wordcount.iterms(), key = itemgettter(0))
for word,count in sorted_wordcount:
    print("%s\t%s") %(word, count)</pre> 

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档