【发布时间】:2014-11-11 00:33:24
【问题描述】:
我尝试为给定文件编写字数统计代码。当我运行这个时,我的字典里是空的,我只是想只得到单词和它的频率。我不确定这是哪里错了。
import collections, re
class Wordcount(object):
def __init__(self):
self.freq_dict = collections.defaultdict(int)
def count(self,input_file):
with open(input_file) as f:
for line in f:
words = line.rstrip().strip().split()
for word in words:
word = word.lower()
word = re.sub("[^A-Za-z0-9]+",'',word)
self.freq_dict[word]+=1
print self.freq_dict
def Main():
c1 = Wordcount()
c1.count('out.txt')
我的out.txt是这样的
The quick brown fox jumps over the lazy dog
--
asd
asdasd
The quick brown fox jumps over the lazy dog's
The quick brown fox jumps over the lazy dog
asd to 之前的空格被解析到字典中。
defaultdict(<type 'int'>, {'': 1, 'brown': 3, 'lazy': 3, 'over': 3, 'fox': 3, 'dog': 2, 'asdasd': 1, 'dogs': 1, 'asd': 1, 'quick': 3, 'the': 6, 'jumps': 3})
我还想将这部分用于 ssh 扩展到近 1000 台机器并读取文件并增加单词的频率。什么是最好的方法?我是否应该创建一个线程 T1 用于登录机器并将登录名传递给另一个线程以读取文件,然后传递给另一个线程以单独增加哈希值。
关于如何扩展它的任何建议真的很有帮助吗?
【问题讨论】:
-
Map/Reduce 技术?
-
是的,它的 MR 工作,但我只想使用 Python!
-
避免空行的提示:检查行是否为空。而且只要 strip 就够了,去掉 rstrip。
-
最好使用Counter 进行计数。用于拆分创建迭代器。
标签: python multithreading ssh paramiko