【发布时间】:2011-06-11 22:26:34
【问题描述】:
我正在编写一些 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')
这正是我想做的:
for node in child_nodes:
if node is not None:
parent.appendChild(node)
我是这样写的:
[parent.appendChild(c) for c in child_nodes if c is not None]
我根本不会使用列表结果,我只需要 appendChild 来完成它的工作。
我不是一个很有经验的python程序员,所以我想知道哪个更好? 我喜欢单行解决方案,但我想向有经验的python程序员了解:
在代码美观/可维护性和性能/内存使用方面,哪个更好。
【问题讨论】:
标签: python for-loop python-3.x