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

2024-04-29 10:33:03 发布

您现在位置:Python中文网/ 问答频道 /正文

所以我有以下代码:

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

所以当它在一个列表中循环时,我想为promo\u applied和promo\u not applied创建两个新列表:

所以回报是:

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

Tags: 代码列表forgetnotbuydetailsthing
2条回答
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)
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 _]

相关问题 更多 >