2014-04-03 50 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

什麼效率不高? –