如果之前已经解决过这个问题,我们深表歉意。我找不到任何以前的答案来解决我的具体问题,所以就在这里。
练习要求用户输入 .txt 文件名。该代码获取该文件,并对其中的单词进行计数,创建单词字典:计数对。如果文件已经被输入,并且它的字数被计算在内,那么程序不会重新计算它,而是引用缓存,它存储了以前的计数。
我的问题是创建一个嵌套的字典字典 - 缓存。以下是我到目前为止所拥有的。目前,每个新的 .txt 文件都会重写字典,并防止它被用作缓存。
def main():
file = input("Enter the file name: ") #Takes a file input to count the words
d = {} #open dictionary of dictionaries: a cache of word counts]
with open(file) as f:
if f in d: #check if this file is in cache.
for word in sorted(d[f]): #print the result of the word count of an old document.
print("That file has already been assessed:\n%-12s:%5d" % (word, d[f][word]))
else: #count the words in this file and add the count to the cache as a nested list.
d[f] = {} #create a nested dictionary within 'd'.
for line in f: #counts the unique words within the document.
words = line.split()
for word in words:
word = word.rstrip("!'?.,") #clean up punctuation here
word = word.upper() #all words to uppercase here
if word not in d[f]:
d[f][word] = 1
else:
d[f][word] = d[f][word] + 1
for word in sorted(d[f]): #print the result of the word count of a new document.
print("%-12s:%5d" % (word, d[f][word]))
main() #Run code again to try new file.
main()
请您参考如下方法:
简单修复:
d[file] = {}
....
d[file][word] = 1 # and so on
因为当你 cahnge f
d[f] 仍然引用 d
中的同一个条目
另外,你可以重用defaultdict
:
from collections import defaultdict
d = defaultdict(lambda x: defaultdict(int))
def count(file):
with (open(file)) as f:
if file not in d:
# this is just list comprehension
[d[file][word.rstrip("!'?.,").upper()] += 1
for word in line.split()
for line in f]
return d[file]
def main():
file = input("Enter the file name: ")
count(file)
if file in d:
print("That file has already been assessed, blah blah")
for word in sorted(d[file]): #print the result of the word count of a new document.
print("%-12s:%5d" % (word, d[f][word]))
if __name__ == "__main__":
main()