python中优雅的列表操作

che*_*art 0 python list

在python中,执行以下操作的最佳(pythonic)方式是什么:

你有一份清单.如果列表不为空,则列表中的所有项都保证为字符串.列表中的每个项目都是空字符串,或者True如果isdigit()在项目上调用则保证返回.

从这样的列表开始,最优雅的方式是什么是最终的列表,以便它包含原始列表中的所有项目,空字符串除外?

Mar*_*ers 6

使用filter()默认标识函数(None):

newlist = filter(None, origlist)
Run Code Online (Sandbox Code Playgroud)

或者,列表理解:

newlist = [el for el in origlist if el]
Run Code Online (Sandbox Code Playgroud)