如何从数组内的列表创建两个列表?

时间:2014-10-09 23:13:05

标签: python

所以我有以下代码:

thing = [   ['Promotion not applied', 'Buy1, get 1 for FOR $4.50', '(', 'details', ')'],      [], ['Promotions Applied:', 'BUY 1, GET 1 FOR $4.50', '(', 'details', ')']   ]

因此它遍历列表并且我想为promo_applied和promo_not应用创建两个新列表:

所以回报将是:

 promo_applied=["promotions applied", 'Buy1, get 1 for FOR $4.50']
 promo_not_applied = ["promotion not applied", 'Buy1, get 1 for FOR $4.50']

2 个答案:

答案 0 :(得分:0)

thing = [   ['Promotion not applied', 'Buy1, get 1 for FOR $4.50', '(', 'details', ')'],      [], ['Promotions Applied:', 'BUY 1, GET 1 FOR $4.50', '(', 'details', ')']   ]

promo_applied, _, promo_not_applied = map(lambda s:s[:2], thing)

答案 1 :(得分:0)

thing = [['Promotion not applied', 'Buy1, get 1 for FOR $4.50', '(', 'details', ')'],[],['Promotions Applied:', 'BUY 1, GET 1 FOR $4.50', '(', 'details', ')']]

promo_applied = [_[:2] for _ in thing if 'Promotions Applied:' in _]
promo_not_applied = [_[:2] for _ in thing if 'Promotion not applied' in _]