1

2つの1Dリストがあるとしましょう

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

今、私は listComprehension を返すレシピを探していますがfirstListsecondListNone オブジェクトはありません。

だから、それはこのように見えるはずです

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 に答える