gpt4 book ai didi

python - 在 python 中迭代元素子元素

转载 作者:行者123 更新时间:2023-11-28 23:02:53 24 4
gpt4 key购买 nike

我通常会做以下事情:

nextPreset = element.firstChild
while nextPreset != None:
#doThings
nextPreset = nextPreset.nextSibling

我想知道是否有类似的东西:

for child in element.children:
#doThings

我看到一个方法_get_childNodes,但是它是私有(private)的...

最佳答案

如果你在一个项目中多次这样做,你可以使用

def iterate_children(parent):
child = parent.firstChild
while child != None:
yield child
child = child.nextSibling

然后做

for child in iterate_children(element):
# foo

关于python - 在 python 中迭代元素子元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9311337/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com