Question

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

in the data list there are SUBLISTS that each contain 3 sublists

in this case I have the data list that contains 3 SUBLISTS and each of those SUBLISTS has 3 sublists . . . of size 3,2, and 1, respectively.

#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'

basically what i'm trying to do is create a new list that gives

#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 should be:

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

i've been trying this for a while and i haven't gotten very far . it was difficult enough to get the data in this format . any help would be greatly appreciated

Was it helpful?

Solution

May not be efficient, but is readable:

>>> [[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 returns True if one element of the list is true, and int(True) is 1.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top