2014-04-03 46 views
0
data = [ [['name1','name1','name1'],[0,0,2],[1000,1200,30],['--','No','Yes']],[['name2','name2'],[0,0,0],[1000,1100],['--','No']], [['name3'],[1],[1234],['--']] ] 

在数据列表有子列表,每个包含3个子列表从子列表创建布尔列表。 Python 2.7版

在这种情况下,我有一个包含3个子列表而且每个子列表中有3子列表中的数据表。 。 。大小分别为3,2和1。

#sublist1 has repeating name strings (of X elements) *all of these elements are the same 
#sublist2 has a series of integers (of X elements) 
#sublist3 has a series of integers (of X elements) 
#sublist4 has a series of strings (of X elements) *these elements can include 'No','--', or 'Yes' 

基本上就是我想要做的就是创建一个新的列表,它

#1) item[0] the name in the first sublist 
#2) item[1] a '1' if at least one of the values in sublist2 is > 0 . . . or else print a '0' 
#3) item[2] a '1' if at least one of the values in sublist3 is >= 1200 . . . or else print a '0' 
#4) item[3] a '1' if at least one of the strings in sublist4 == 'Yes' . . . or else print a '0' 

输出应该是:

output = [ ['name1','1', '1','1'],['name2','0','0','0'],['name3','1','1','0'] ] 

我一直想了一会儿,并我还没有走得很远。很难以这种格式获取数据。任何帮助将不胜感激

+1

,你能否告诉我们你到目前为止制作了什么? –

回答

1

效率不高,但可读性:

>>> [[i[0][0], 
... int(any([1 for j in i[1] if j>0])), 
... int(any([1 for j in i[2] if j>=1200])), 
... int(any([1 for j in i[3] if j=='Yes']))] for i in data] 
[['name1', 1, 1, 1], ['name2', 0, 0, 0], ['name3', 1, 1, 0]] 

any返回true如果列表中的一个元素是真实的,int(True)为1

+0

什么效率不高? –