аватар question@mail.ru · 01.01.1970 03:00

Remove the list elements under the condition

there is a list

  lst = [ 0 ,  95  0 ,  76 ,  0 ,  23 ,  68 ,  0 ,  23 ,  156 ,  95 ]  

you need to delete each element following 0

to get on output:

  New_lst = [ 0 ,  0 ,  0 ,  68 ,  0 ,  156 ,  95 ]     

method str.index () does not help me, since only the first coincidence retus, and C enumerate does not work out.

аватар answer@mail.ru · 01.01.1970 03:00
  LST = [ 0 ,  95 ,  0 ,  76 ,  0 ,  23 ,  68  0  23 ,  156 ,  95 ]  # since there is nothing before the first element # that corresponds to the condition),   # then it is added in any case.   # to the same at I == 0, I -1 == -1 exceptions,   # This is the last element of the list   # through the cycle  new_list = []   for  i  in   range  ( len  (lst)):   if  i ==  0   ry  lst [I -  1 ]! =  0 :  New_list.ppend (lst [i])   print  (New_list)  # through Generator    print  ([lst [i]  for  i  in   range  ( len  (lst))  if  i ==  0   or  lst [i -  1 ]! =  0 ])                                       

Latest

Similar