小编And*_*ske的帖子

带循环的Python循环

我正在写一些Python 3.2代码,这个问题来找我:

我有这些变量:

# a list of xml.dom nodes (this is just an example!)
child_nodes = [node1, node2, node3]
# I want to add every item in child_node into this node (this also a xml.dom Node)
parent = xml_document.createElement('TheParentNode')
Run Code Online (Sandbox Code Playgroud)

这正是我想要做的:

for node in child_nodes:
    if node is not None:
        parent.appendChild(node)
Run Code Online (Sandbox Code Playgroud)

我在一行中写了这样的:

[parent.appendChild(c) for c in child_nodes if c is not None]
Run Code Online (Sandbox Code Playgroud)

我根本不打算使用列表结果,我只需要appendChild来完成它的工作.

我不是一个非常有经验的python程序员,所以我想知道哪个更好?我喜欢单线解决方案,但我想从经验丰富的python程序员那里了解到:

在代码美/可维护性和性能/内存使用的上下文中哪一个更好.

python for-loop python-3.x

0
推荐指数
1
解决办法
180
查看次数

标签 统计

for-loop ×1

python ×1

python-3.x ×1