好的,我知道如何计算字符串(o = len(x.split()) print(o)
)中有多少个单词,但如何计算.txt文件中有多少个单词
顺便说一句,我的代码如下所示:
p = open("einsteinsbiography", "r", encoding="utf8")
x = dict()
for line in p:
line = line.strip()
line = line.lower()
t = line.split(" ")
for word in t:
if word in x:
x[word] = x[word] + 1
else:
x[word] = 1
for key in list(x.keys()):
print(key, ":", x[key])
内存友好的细微变化(它逐行迭代.txt文件,而不是一次加载所有文件):
如果你想计算每个单词的实例数,我推荐big_bad_bison的答案,并使用计数器对象
文本文件中的字数
要仅获取文件中的总字数,请执行以下操作:
如果要获取包含文件中每个单词计数的dict,请使用Counter:
相关问题 更多 >
编程相关推荐