【问题标题】:How to append a empty list within for loop in Python如何在 Python 的 for 循环中附加一个空列表
【发布时间】:2016-02-23 05:59:16
【问题描述】:
a=np.where(MACD[2]>0.)[0]
b=np.where(MACD[2]<0.)[0]
num=np.array([1,2])
num1=[]
for x in range (-1,-30,-1):
    while True:
       if(a[x]-a[x-1]>1):
          num1.append(num)
          print num1
          break

我想附加 num 但是当我运行它时它挂在那里。 我想要的输出是 num=[45,74,41,1,2] 这意味着 num1 在 for 循环中每次运行时都不会被覆盖。

【问题讨论】:

  • while 循环的目的是什么?

标签: python numpy


【解决方案1】:
a=np.where(MACD[2]>0.)[0]
b=np.where(MACD[2]<0.)[0]

num=np.array([1])
num1=[]

for x in range (-1,-30,-1):#for MACD above  0
    if(a[x]-a[x-1]>1):
        num=a[x]
        num1.append(num)
        print num1

发现额外的 num=a[x] 就可以了

【讨论】:

    猜你喜欢
    • 2019-04-05
    • 2020-11-22
    • 2020-03-26
    • 2020-10-14
    • 2020-07-26
    • 2021-06-20
    • 1970-01-01
    • 2018-12-11
    • 1970-01-01
    相关资源
    最近更新 更多