循环列表中断并继续 [英] for loop over list break and continue

查看:162
本文介绍了循环列表中断并继续的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

正确地指出问题:我为这个混淆而道歉$ ​​b $ b怀疑早期从循环中断开。
我有文件夹 - 1995,1996至2014年。每个文件夹都有xml文件。在一些xml文件中,入口 - MaxAmdLetterDate是None。
我想进入文件夹中的下一个文件,而现在使用break,程序执行到下一个文件夹,并在遇到中断时跳过该文件夹中的所有文件
这是代码

 导入xml.etree.ElementTree作为ET 
导入os
用于范围内的yrcnt(1995,2015 ):
old = old =./ old /+ str(yrcnt)
用于dirpath,subdirs,os.walk中的文件(旧):存在子文件夹>文件的#folder名称
打印文件
用于文件中的文件名:
if filename.endswith(。xml)== True:
fl =文件名
打印fl
打印-----读取当前文件-------+ str(fl)
flnm = str(dirpath)+/+ str(文件名)
tree1 = ET.parse (flnm)#db在系统
root1 = tree1.getroot()
在ch1中root1.findall('Award'):
ai文本
print最大值日期:+ str(maxdate1)
如果maxdate1(最大值)
最大值1 = ch1.find('MaxAmdLetterDate' ==无:
break

帮助表示感谢。



ORIGINAL POST:
我的for循环在遇到中断时得到提前退出。任何补救措施?

  files = [1,2,3,4,5] 
用于文件名称:
打印文件名
如果文件名== 3:
break

o / p:

  1 
2
3

预计的O / P

  1 
2
4
5

谢谢!
$ b $编辑:有一些评论,如这是休息的预期功能。让我澄清一下:我希望函数类似于范围(0,5),Iam使用这个作为嵌套循环的一部分,我希望循环继续,而不是由于一个文件而退出。

解决方案

这就是 break 的工作方式。如果你想迭代整个列表,只要删除 break (或者如果你想对值做些什么,可以使用 pass 作为占位符)。

  values = [1,2,3,4,5] 
对于值中的值:
if value == 3:
pass
print(value)

输出:

  1 
2
3
4
5

如果您只想跳过某个值,请使用

  values = [1,2,3,4,5] 
值为值:
if value == 3:
continue
print(value)

输出:

  1 
2
4
5


To specify the problem correctly :i apologize for the confusion Having doubts with breaking early from loop . I have folders - 1995,1996 to 2014 . Each folder has xml files. In some xml files the entry - MaxAmdLetterDate is None. I want to proceed to the next file in the folder, instead right now with break , the program execution goes to the next folder and skips all files in that folder when it encounters a break This is the code

import xml.etree.ElementTree as ET
import os
for yrcnt in range(1995,2015):
    old= old="./old/"+ str(yrcnt)
    for dirpath, subdirs, files in os.walk(old): #folder name in which subfolder>files are present
        print files
        for filename in files:
            if filename.endswith(".xml") == True:
                fl=filename
                print fl
                print "-----Reading current file -------"  + str(fl)
                flnm=str(dirpath)+"/"+str(filename)
                tree1 = ET.parse(flnm)                        #db on system
                root1 = tree1.getroot()
                for ch1 in root1.findall('Award'):
                    aid1 = ch1.find('AwardID').text
                    maxdate1=ch1.find('MaxAmdLetterDate').text
                    print "Max amd date : "+str(maxdate1)
            if maxdate1==None:
                break

Help is appreciated .

ORIGINAL POST: My for loop gets an early exit on encountering a break . Any remedies ?

files=[1,2,3,4,5]
for filename in files:
  print filename
  if filename==3:
     break

o/p:

  1
  2
  3

expected O/p

  1
  2
  4
  5

Thanks !

EDIT: With some comments such as this is expected function of break . Let me elucidate: I want the function to be similar to a range(0,5) , Iam using this as a part of nested loop where I want the loop to go on , and not exit out due to one file .

解决方案

This is the way break works. If you want to iterate over the whole list, simply remove the break (or if you want to do something with the value, use pass as a placeholder).

values = [1, 2, 3, 4, 5]
for value in values:
    if value == 3:
        pass
    print(value)

Output:

1
2
3
4
5  

If you just want to a skip a value, use continue keyword.

values = [1, 2, 3, 4, 5]
for value in values:
    if value == 3:
        continue
    print(value)

Output:

1
2
4
5 

这篇关于循环列表中断并继续的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆