问题
Could someone help with a logical scheme in for loop statement in Python? I try to be more clear: i want to build a for loop cycle where if a condition is TRUE i want to append the same value for the next cells until another condition become TRUE.
For example: i have a dataframe called df with 3 columns. The first column is the closing price of a stock (close_price), the second column contains the fast exponential moving average (fast_ema) and the third column contains the slow exponential moving average (ema_slow). At this point I run the code below:
list = []
for i in range(1,len(df)):
# FIRST CONDITION:
if (df.ema_fast[i-1] < df.ema_slow[i-1] and df.ema_fast[i] > df.ema_slow[i]:
list.append(df.close_price[i]) # i want to append close.close[i] for al the next cells i (i+1, i+2,...)
#until the SECOND condition become TRUE.
# SECOND CONDITION:
elif if (df.fast_ema[i-1] > df.ema_slow[i-1] and df.ema_fast[i] > df.ema_slow[i]:
list.append(df.close_price[i])
else:
list.append(0)
when the short ema cross over the slow ema this code append the close_price in the list and then, if the ema_fast cross down the ema_slow the code appends the close_price when the crossdown is occured. Otherwise the code appends 0.
A this point, if I assume that cross over occured in data 2019-08-19 and the cross down occured in data 2019-08-27, I get:
data Price
2019-08-19 df.close_price[i=2019-08-19] # The closing price in data 2019-08-19
xxxx-xx-xx 0
xxxx-xx-xx 0
xxxx-xx-xx 0
xxxx-xx-xx 0
xxxx-xx-xx 0
xxxx-xx-xx 0
xxxx-xx-xx 0
2019-08-27 df.close_i[i=2019-08-27] # The closing price in data 2019-08-27
xxxx-xx-xx 0
xxxx-xx-xx 0
Now i want:
2019-08-19 df.close_price[i=2019-08-19] # The close in data 2019-08-19
xxxx-xx-xx df.close_price[i=2019-08-19] # The close in data 2019-08-19
xxxx-xx-xx df.close_price[i=2019-08-19] # The close in data 2019-08-19
xxxx-xx-xx df.close_price[i=2019-08-19] # The close in data 2019-08-19
xxxx-xx-xx df.close_price[i=2019-08-19] # The close in data 2019-08-19
xxxx-xx-xx df.close_price[i=2019-08-19] # The close in data 2019-08-19
xxxx-xx-xx df.close_price[i=2019-08-19] # The close in data 2019-08-19
xxxx-xx-xx df.close_price[i=2019-08-19] # The close in data 2019-08-19
2019-08-27 *df.close_price[i=2019-08-27]* # The close in data 2019-08-27
xxxx-xx-xx 0
xxxx-xx-xx 0
I'm not a python expert and I hope I was clear enough. Thank you in advance, and if someone decide to help me I will be very grateful.
回答1:
You can use a variable, here adding
, to both note that you have encountered condition 1, remember the value you got then, and also to see that you have not encountered condition 2 yet, so can keep adding it:
adding = None
for i in range(1,len(close)):
if first_condition():
adding = close.close[i]
list.append(adding) # i want to append close.close[i] for al the next cells i (i+1, i+2,...)
#until the SECOND condition become TRUE.
# SECOND CONDITION:
elif if (close.fast_ema[i-1] > close.int_ema[i-1] and close.fast_ema[i] > close.slow_ema[i]:
list.append(close.close)
adding = None
else:
if adding is not None:
list.append(adding)
else:
list.append(0)
来源:https://stackoverflow.com/questions/63919473/python-for-loop-how-to-append-the-true-valuei-in-the-next-cellsi1-i2