mardi 29 août 2017

Drop multiple elements from a list (drop row2 from list of lists), conditional on a value in a different row

I have a list of lists. Example here:

    list_of_data = 
['Joe', 4, 4, 4, 5, 'cabbage', None], 
['Joe', 43, 2TM, 41, 53, 'cabbage', None],
['Joe', 24, 34, 44, 55, 'cabbage', None],
['Joe', 54, 37, 42, 85, 'cabbage', None],

['Tom', 7, 2TM, 4, 52, 'cabbage', None],
['Tom', 4, 24, 43, 52, 'cabbage', None],
['Tom', 4, 4, 4, 5, 'cabbage', None],

['Fred', 4, 4, 4, 5, 6, 'cabbage'],
['Fred', 4, 4, 4, 5, 6, 'cabbage'],
['Fred', 4, 4, 4, 5, 6, 'cabbage']]

Notice that for index #2, Joe has value '2TM' in the 2nd row of his list (row index #1 of the overall list_of_data). Tom has value '2TM' in the 1st row of his list (row index #5 of the overall list_of_data).

Each tie the value '2TM' appears in my data, I want to remove/delete/skip the next two rows.

So here is my final desired output:

    list_of_data = 
['Joe', 4, 4, 4, 5, 'cabbage', None], 
['Joe', 43, 2TM, 41, 53, 'cabbage', None],

['Tom', 7, 2TM, 4, 52, 'cabbage', None],

['Fred', 4, 4, 4, 5, 6, 'cabbage'],
['Fred', 4, 4, 4, 5, 6, 'cabbage'],
['Fred', 4, 4, 4, 5, 6, 'cabbage']]

I don't know if I'm even close on the code, but I've tried using the .pop method like so:

for row[x] in list_of_data:
    if '2TM' in row:
        list_of_data.pop[x+1:x+2]

Aucun commentaire:

Enregistrer un commentaire