1

假设我有两个一维列表

firstList = [ "sample01", None, "sample02", "sample03", None ]
secondList = [ "sample01", "sample02", "sample03", None, None, None, "sample04"]

现在我正在寻找将返回但没有 None 对象的 listComprehension 的firstList配方secondList

所以它应该看起来像这样

listComprehension_List = [  [ "sample01","sample02","sample03" ] ,  [ "sample01","sample02","sample03", "sample04"  ]     ]
listComprehension_List = [[firstList without NONE objects],[secondList without NONE objects]]

我期待任何输入......现在我将继续尝试!

4

1 回答 1

5
>>> firstList = [ "sample01", None, "sample02", "sample03", None ]
>>> secondList = [ "sample01", "sample02", "sample03", None, None, None, "sample04"]

带有列表组合

>>> [x for x in firstList if x is not None]
['sample01', 'sample02', 'sample03']

或者你可以使用filter

>>> filter(None, secondList)
['sample01', 'sample02', 'sample03', 'sample04']

对彼此而言:

>>> [[y for y in x if y is not None] for x in (firstList, secondList)]
[['sample01', 'sample02', 'sample03'], ['sample01', 'sample02', 'sample03', 'sample04']]
于 2013-06-14T12:57:43.810 回答