python - Writing two objects simultaneously -


i need calculate 2 interdependent values @ same time. problem a , b depend on previous value of a , b. need calculate them simultaneously while referring last calculated values of loop. have far this:

   x = df.x # list containing randomly 1 , 0    df['a']=100 # 100 starting value , b , shall overwritten while loop    df['b']=100    i=1        while i<len(df.index):        df.a[i] = x*((df.a.shift(1)*0.45)+(df.b.shift(1)*0.5))+abs(1-x)*df.a.shift(1)        df.b[i] = x*((df.b.shift(1)*0.5)+(df.a.shift(1)*0.59))+abs(1-x)*df.b.shift(1)        i+1 

df dataframe. error: valueerror: setting array element sequence.

i know why getting this problem, see this question. question is, how can solve this? there more efficient solution while loop...

i can note df.a , df.b both sequences. use map obtain result.

example:

l = [1, 2, 3] set_negative = lambda x: x * -1  # function o lammbda receives sinble argument ml = map(set_negative, l) # ml = [-1, -2, -3] 

in case write code this:

in_a = lambda _: x*((df.a.shift(1)*0.45)+(df.b.shift(1)*0.5))+abs(1-x)*df.a.shift(1) in_b = lambda _: x*((df.b.shift(1)*0.5)+(df.a.shift(1)*0.59))+abs(1-x)*df.b.shift(1)  df.a = map(in_a, df.a) df.b = map(in_b, df.b) 

you try more sofisiticated solutions using: starmap or imap


Comments