Python,For循环:如何在下一个单元格[i + 1,i + 2,..]中附加TRUE值[i],直到条件变为FALSE [英] Python, For loop: How to append the TRUE value[i] in the next cells[i+1,i+2,..] until the condition become FALSE

查看:147
本文介绍了Python,For循环:如何在下一个单元格[i + 1,i + 2,..]中附加TRUE值[i],直到条件变为FALSE的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人可以在Python的for循环语句中提供逻辑方案帮助吗?我尝试更加清楚:我想建立一个for循环,如果条件为TRUE,我想为下一个单元格附加相同的值,直到另一个条件变为TRUE.

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.

例如:我有一个名为df的数据框,具有3列.第一列是股票的收盘价(close_price),第二列是快速指数移动平均线(fast_ema),第三列是慢速指数移动平均线(ema_slow).此时,我运行以下代码:

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)


当短ema与慢ema交叉时,此代码在列表中附加close_price,然后,如果ema_fast与ema_slow交叉,则代码在发生交叉时附加close_price.否则,代码将附加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.

至此,如果我假设交叉发生在数据2019-08-19中,而交叉发生在数据2019-08-27中,我将得到:

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

现在我想要:

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

我不是python专家,我希望我足够清楚.预先谢谢您,如果有人决定帮助我,我将非常感激.

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.

推荐答案

您可以在此处使用 adding 变量,以注意您遇到了条件1,请记住当时得到的值,并且还可以看到您还没有遇到条件2,因此可以继续添加它:

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)

这篇关于Python,For循环:如何在下一个单元格[i + 1,i + 2,..]中附加TRUE值[i],直到条件变为FALSE的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆