跳出两层循环,跳出两层,python没有提供go


python没有提供goto类似的语法特性,平常跳出两层循环一般是把两层循环包装在一个函数里,用return得方式直接返回,或者设置一个flag,今天在stackoverflow上看到一个实现方式,感觉挺好得,地址: http://stackoverflow.com/questions/189645/how-to-break-out-of-multiple-loops-in-python

for a in xrange(10):    for b in xrange(20):        if something(a, b):            # Break the inner loop...            break    else:        # Continue if the inner loop wasn't broken.        continue    # Inner loop was broken, break the outer.    break#该片段来自于http://byrx.net

评论关闭