break在哪里结束For循环

时间:2014-04-22 19:16:16

标签: python for-loop break

我的代码接收一个文本文件并解析它以获取每个单词的频率并将其存储在字典中。

# 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

我的问题是我需要休息才能结束检查整个字符串,而不仅仅是结束检查字符。

break是否结束它所在的循环或结束第一个循环:(" for f")

而且,继续只会结束那个特定的循环。

我想要它,以便它结束检查整个字符串,然后移动到单词中的下一个字符串。

3 个答案:

答案 0 :(得分:1)

来自documentation

  

break语句就像在C中一样,突破了最小的封闭   for或while循环。

根据this question,Python不像PHP那样支持像break 2这样的构造。

答案 1 :(得分:1)

break结束它所包含的最内层/立即循环,即直接在其范围内的循环。

for x in X:
    for y in Y:
        break

x循环将运行完成,y循环将中断。

您可以通过将变量设置为标志来导致外部循环中断:

break_outer = False
for x in X:
    for y in Y:
        if condition:
            break_outer = True
            break
    if break_outer:
        break

示例:

for x in range(3):
    for y in range(2):
        if x == 2:
            break
        print "x =",x,"y =",y

输出:

>>> x = 0 y = 0
>>> x = 0 y = 1
>>> x = 2 y = 0
>>> x = 2 y = 1

要打破外部循环,你可以传递一个变量:

break_outer = False
for x in range(3):
    for y in range(2):
        if x == 2:
            break_outer = True
            break
        print "x =",x,"y =",y
    if break_outer:
        break

输出:

>>> x = 0 y = 0
>>> x = 0 y = 1

continue跳过循环中剩余的其余代码,继续for循环中的下一次迭代:

for i in range(3):
    if i == 1:
        continue
    print i

输出:

>>> 0
>>> 2

您的代码似乎按照您的要求执行,break并转到下一个单词...是否有其他关于代码产生不良结果的内容?

答案 2 :(得分:1)

break将突破它所处的最内层循环。

您可以使用@farmerjoe建议的内容来突破外部循环。

但我不明白你为什么要那样做。您所拥有的break似乎很好:它将停止处理当前字符串的字符,然后继续检查下一个字符串。

您的代码中存在一些错误。我为你修理了一些风格问题:

def load_model(filename):
    dictionary = {}
    with open(filename) as f:
        for line in f:
            words = line.split(' ')  # Split each word and iterate through.
            for word in words:
                for c in word:  # Check each character value for punctuation or numeric type.
                    if c in Punct:
                        word = word.replace(c, "")
                    if c.isdigit():
                        print word + ' is not formattable.\n'
                        break

                word = word.lower()
                if word in dictionary:
                    dictionary[word] += 1
                else:
                    dictionary[word] = 1
    return dictionary