问题描述
该程序的目的是计算段落中的每个单词并记录频率。 不幸的是,该程序还在计算空字符串。 我的代码是:
def build_map( in_file, word_map ):
# Receives an input file and an empty dictionary
for line in in_file:
# Splits each line at blank space and turns it into
# a list.
word_list = line.split()
for word in word_list:
word= word.strip().strip(string.punctuation).lower()#program revised
if word!='':
# Within the word_list, we are stripping empty space
# on both sides of each word and also stripping any
# punctuation on both side of each word in the list.
# Then, it turns each word to the lower case to avoid
# counting 'THE' and 'the' as two different words.
add_word( word_map, word)
我真的很感激,如果有人可以看看代码并解释,为什么它仍在计算空字符串。 除此之外,其他一切工作正常。 谢谢(修改了代码,现在工作正常)。
1楼
您正在检查该单词是否为空, 然后您正在剥离空白和标点符号。 颠倒这些操作的顺序。