Python字计数器只计算一次字
我正在尝试制作一个Python单词计数器,它对输入到字典中的文件中的单词进行计数。然而,我的计数器只计算一次单词,我不知道为什么。还有,有没有办法不使用收款台Python字计数器只计算一次字,python,python-3.x,python-3.6,Python,Python 3.x,Python 3.6,我正在尝试制作一个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_
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
这将重置单词新事物的计数器
由于您已经使用了cloud.get(new\u thing,0)
,如果找不到键new\u thing
,它将返回0
,您可以删除该行。除了将每个“new\u thing”的值初始化为0(cloud[new\u thing]=0
)之外,正如其他人所指出的,还有一个主要问题:在向云添加任何元素之前,您尝试迭代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 dictionary的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:对于行中的内容:
不以文字分隔行,而是以字符分隔行。我猜你指的是线中的东西。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)
每次你遇到这个词时,你都会立即将所有其他出现的次数重置为0:
cloud[new\u thing]=0
你可能想从collections模块中查看一下计数器Dict,这会为你节省一些工作。你可以使用Counter(list),它会给出一个包含所有单词的字典以及列表中的单词数。你能给出一个简单的文件示例和预期结果吗。也许有更好的方法来解决同样的问题,比如“牙刷、梳子、钢笔、铅笔”之类的文件对象名称,有些名称会重复多次。我的目标是计算对象出现的次数。所以,如果我看到牙刷两次,它将是->牙刷:2“打印测试所有单词是否正确计数”和打印字典之间的区别是什么?你能显示你用来打印的代码吗?