我有一個代碼,它接受一個文本文件並解析它以獲取每個單詞的頻率並將其存儲在字典中。在哪裏結束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
休息結束最內層循環。 –