for循环中的变量将成为外部循环Python的新迭代器

问题描述 投票:0回答:1

假设我有以下示例代码:

element = xmlDoc.find("...") # this returns an element that is found within an XML document
for child in element.getchildren():
    # iterate over each child
    # do some stuff
    if some_condition: # assume that at some point in the loop, this condition is executed
        element = xmlDoc.find("..." # find a new element in the doc and this element should be the new element to iterate over from the next loop

这显然是非常理论上的。我想做的是通过查看某个“元素”节点的每个子节点开始一个循环。但是,如果执行了some_condition(它将在我的代码中的某个时刻),那么我希望下一个for循环迭代在该if语句中使用新的“ element”变量。因此,我希望下一个循环迭代遍历“ new”元素的每个子节点,而不是从第一次迭代开始的那个子节点。

有什么办法可以做到这一点?

假设我有以下示例代码:element = xmlDoc.find(“ ...”)#这返回一个在XML文档中为element.getchildren()中的child元素找到的元素:#遍历每一个...

python xml lxml
1个回答
1
投票

我认为这是您想要的:

© www.soinside.com 2019 - 2024. All rights reserved.