2014-10-09 215 views
-1

所以我有一个下面的代码:我如何从数组中的列表创建两个列表?

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'] 
+0

而且?有什么问题?你已经有了这两个列表(加上一些其他的东西),那么你真正的问题是什么?你知道如何访问列表元素吗?顺便说一下,这感觉非常[XY-ish](http://meta.stackexchange.com/q/66377/214517)对我来说 - 这些列表来自哪里? – 2014-10-09 23:25:48

+0

'thing'列表是否总是包含3个元素? 'thing'中是否有多于一个可能出现的以''没有应用促销''或''应用促销'开头的列表? – 2014-10-09 23:27:03

+0

我认为你应该退一步,更详细地设计你的程序。尤其是,您应该了解可用于表示每次促销的类和对象。 – 2014-10-16 23:15:24

回答

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) 
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 _]