python - A loop for adding list element without high memory performance? -


everybody,

a =[0, 0, 2, 4, 6]  x=5 

this list (a) , fix value (x).

i need loop codes must add x every element of list , add value previous list elements in every loop ( loop must continue x value). other words result should below:

0 0 2 4 6 0 0 7 9 11 0 0 12 14 16 0 0 15 19 21 0 0 21 24 26 

i prepared codes below doesn’t work. other words produce below (incorrect)

i=0 counter=0 while counter < x:   in a:      if >0:         i=i+x     elif ==0:         i=0     print   counter=counter+1 

0 0 7 9 11 0 0 7 9 11 0 0 7 9 11 0 0 7 9 11 0 0 7 9 11

so, need this… thank you.

i think want (at least, understand question)...

def make_it_so(a, x):   = 0   counter=0   while counter < x:     in a:       if == 0:         yield 0       else:         yield + counter * x     counter = counter + 1  # demo item in make_it_so([0, 0, 2, 4, 6], 5):     print item 

note i've made generator function. turn regular function returns list if created output list @ top of function , swapped yield ... output_list.append(...) , return output_list @ end of function...

the key here understand in first loop, adding 0 of (non-zero) items. in second loop, adding x. in third loop, you're adding x + x (since first loop added x , you're adding x more). in general, nth loop, you'll adding (n-1) * x of non-zero items. so, need keep track of n, (or n-1). in fact, original code doing (with counter), re-purpose , it's good.