[英]Python Word Counter Only Counting Words Once
我正在尝试创建一个Python字计数器,用于计算输入字典的文件中的单词。 但是,我的计数器只计算一次这个词,我不知道为什么。 还有,有没有办法不使用收集柜台?
cloud = {}
val = 0
with open('objects.txt', 'r') as file:
for line in file:
for thing in line:
new_thing = thing.strip(' ')
cloud[new_thing] = val
for new_thing in cloud:
cloud[new_thing] = cloud.get(new_thing, val) + 1
在您的代码中,为每个新行设置
cloud[new_thing] = 0
这会重置new_thing
这个词的计数器。
由于您已经使用了cloud.get(new_thing, 0)
,如果找不到密钥new_thing
将返回0
,您可以删除该行。
除了将其他每个“new_thing”的值初始化为0( cloud[new_thing] = 0
)之外,还有另一个主要问题:在向其添加任何元素之前尝试迭代cloud
(因此, for new_thing in cloud:
它的块实际上什么都不做,因为cloud
是空的)。 这不是必需的,因为字典是非顺序访问的。
你可以替换
new_thing = thing.strip(string.punctuation)
cloud[new_thing] = 0
for new_thing in cloud:
cloud[new_thing] = cloud.get(new_thing, 0) + 1
只是:
new_thing = thing.strip(string.punctuation)
cloud[new_thing] = cloud.get(new_thing, 0) + 1
或者使用collections.Counter
,正如其他人所建议的那样,已经完成了你想要完成的任务,并且可能会让你的任务变得更容易。
你可以使用python字典的setdefault
函数
for new_thing in cloud:
count = cloud.setdefault(new_thing, 0)
cloud[new_thing] = count + 1
我将提取以行和单词分割文件的部分,并删除标点符号:
def strip_punctuation(lines):
for line in lines:
for word in line:
yield word.strip(string.punctuation)
with open('objects.txt', 'r') as file:
cloud = collections.Counter(strip_punctuation(file))
或者,使用itertools.chain
和map
更简洁:
with open('objects.txt', 'r') as file:
words = itertools.chain.from_iterable(file)
words_no_punctuation = map(lambda x: x.strip(string.punctuation))
cloud = collections.Counter(words_no_punctuation)
PS: for thing in line:
不会在单词中分割行,而是在字符中。 我想你的意思是for thing in line.split():
然后最后一个选项变成:
with open('objects.txt', 'r') as file:
words_per_line = map(lambda line: line.split(), file)
words = itertools.chain.from_iterable(words_per_line)
words_no_punctuation = map(lambda x: x.strip(string.punctuation))
cloud = collections.Counter(words_no_punctuation)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.