And*_*ske 0 python for-loop python-3.x
我正在写一些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程序员那里了解到:
在代码美/可维护性和性能/内存使用的上下文中哪一个更好.