请问各位老师,这种情况我该怎么办:

2021-02-20 11:55发布

def getState(data, t, n): d = t - n + 1 # block = data[d:t + 1] if d>= 0 else -d * [data[0]]+ data[0:t+1] block = data[d:t + 1] #res = [0 for x in range(0, len(n))] res = [] for i in range(n - 1): res.append(block[i + 1]-block[i]) #print("res=",res[i]) return np.array([res])

 

之后它说我:    res.append(block[i + 1]-block[i]) IndexError: list index out of range Episode1

 

然后我把代码改成了:

def getState(data, t, n): d = t - n + 1 # block = data[d:t + 1] if d>= 0 else -d * [data[0]]+ data[0:t+1] block = data[d:t + 1] res = [0 for x in range(0, len(n))] #res = [] for i in range(n - 1): res.append(block[i + 1]-block[i]) #print("res=",res[i]) return np.array([res])

 

然后它说我:  res = [0 for x in range(0, len(n))] TypeError: object of type 'int' has no len()

那么请问各位老师我到底该怎么改

6条回答
weixin_54575587
1楼-- · 2021-02-20 12:47

可是,现在还有个问题,

def getState(data, t, n):
    d = t - n + 1
    # block = data[d:t + 1] if d>= 0 else -d * [data[0]]+ data[0:t+1]
    block = data[d:t + 1]
    res = [0 for x in range(0, n)]
    #res = []
    for i in range(n - 1):
        res.append(block[i + 1]-block[i])
        #print("res=",res[i])
        return np.array([res])

 

结果现在又出现这种问题:

  File "C:/Users/86138/Desktop/stockPrediction-master/yunfuzai_main.py", line 79, in getState
    res.append(block[i + 1]-block[i])
IndexError: list index out of range

上一次就是过界,改完后又是过界,请问这种情况如何解决呢

研必行
2楼-- · 2021-02-20 12:51

这样??

HENU小菜鸡
3楼-- · 2021-02-20 12:53
def getState(data, t, n):
    d = t - n + 1
    # block = data[d:t + 1] if d>= 0 else -d * [data[0]]+ data[0:t+1]
    block = data[d:t + 1]
    res = [0 for x in range(0, n)]
    # res = []
    for i in range(len(block) - 1):
        res.append(block[i + 1]-block[i])
        #print("res=",res[i])
    return np.array([res])

 

梦蝶—Nydia
4楼-- · 2021-02-20 12:54

你改成 [0 for x in range(0, n)]  

Remote Sensing_
5楼-- · 2021-02-20 12:55

就是想把代码运行通顺,把block加到res

Lizzy_0323
6楼-- · 2021-02-20 12:56

你具体要做什么?