【发布时间】:2021-10-16 23:49:00
【问题描述】:
我希望文本是一个键:每行计数。 现在它将文件保存为普通字典,我无法弄清楚。
def makeFile(textSorted, newFile) :
dictionary = {}
counts = {}
for word in textSorted :
dictionary[word] = counts
counts[word] = counts.get(word, 0) + 1
# Save it to a file
with open(newFile, "w") as file :
file.write(str(counts))
file.close()
return counts
【问题讨论】:
-
你想要所有的 key:count 在不同的行吗?
-
是的!一键:每行计数。
标签: python dictionary