我有一个代码,它接受一个文本文件并解析它以获取每个单词的频率并将其存储在字典中。在哪里结束For循环
# Load a corpus and build a language model
def load_model(filename):
"""Loads a corpus file and builds a language model consisting of word:frequency.
Converts all words to lowercase, strips punctuation, and ignores any non-alphabetic characters."""
dictionary = {}
f = open(filename)
for line in f:
words = line.split(' ') # Split each word and iterate through.
for string in words:
for c in string: # Check each character value for punctuation or numeric type.
if c in Punct:
string = string.replace(c,"")
if c.isdigit()
print String + ' is not formattable.\n'
break
string = string.lower()
if string in dictionary:
dictionary[string] = dictionary[string] + 1
else:
dictionary[string] = 1
f.close()
return dictionary
我的问题是我需要休息结束检查整个字符串,而不仅仅是结束检查字符。
是否断头路循环它位于或什么时候结束第一循环:(“在F线”)
而且,继续将简单地结束这种特定的循环。
我想要它,以便它结束检查整个字符串,它将移动到单词中的下一个字符串。
我可能不应该使用字符串作为变量名。 – alvarezcl
随时编辑您自己的问题 – niklasfi
休息结束最内层循环。 –