簡體   English   中英

如何修復返回列表中最大增加的 python 代碼的“列表索引超出范圍”錯誤?

[英]How to fix “list index out of range” error for a python code that returns the max increase within a list?

因此,我編寫了這段代碼,以使我從較小元素到較大索引的最大增加,兩者之間的趨勢僅增加,但我遇到了“列表”和“元組索引超出范圍” “當 seq[index + 1] > seq[index]”出現錯誤

所以對於成功的例子:

max_increase((1,2,3,2)) == 2.0

max_increase([3,-1,2,1]) == 3.0

這是我下面的代碼:

def max_increase(seq):
    index = 1
    inc = 0
    diff = 0
    n = 1
    for index in range(1, len(seq)):
        if seq[index] > seq[index - 1]:
            diff = seq[index] - seq[index - 1]
            while seq[index + 1] > seq[index]:
                diff += seq[index + n] - seq[index]
                index = index + 1
            if diff > inc:
                inc = diff
            index = index + 1 
        else:
            index = index + 1
    return inc

不是真正的最低代碼,但首選基本代碼,因為這是一門介紹性編碼課程:)

讓我出錯的測試是:

max_increase([1.0,3.0,1.0,2.0]) == 2.0

btc_data = [ 6729.44, 6690.88, 6526.36, 6359.98, 6475.89, 6258.74,
             6485.10, 6396.64, 6579.00, 6313.51, 6270.20, 6195.01,
             6253.67, 6313.90, 6233.10, 6139.99, 6546.45, 6282.50,
             6718.22, 6941.20, 7030.01, 7017.61, 7414.08, 7533.92,
             7603.99, 7725.43, 8170.01, 8216.74, 8235.70, 8188.00,
             7939.00, 8174.06 ]
btc_data.reverse()
assert abs(max_increase(tuple(btc_data))-589.45) < 1e-6

你放了這樣的額外條件,看起來你有程序錯誤

def max_increase(seq):
    index = 1
    inc = 0
    diff = 0
    n = 1
    for index in range(1, len(seq)):
        if seq[index] > seq[index - 1]:
            diff = seq[index] - seq[index - 1]
            while index != (len(seq)-1) and seq[index + 1] > seq[index]:
                diff += seq[index + n] - seq[index]
                index = index + 1
            if diff > inc:
                inc = diff
            index = index + 1 
        else:
            index = index + 1
    return inc

max_increase([1.0,3.0,1.0,2.0]) == 2.0

這是您的代碼的修訂版。

def max_increase(seq):
    # index = 1
    inc = 0
    diff = 0
    n = len(seq)
    for i in range(1, n):
        for j in range(i, n):
            if seq[j] > seq[i - 1]:
                diff = seq[j] - seq[i - 1]
                if diff > inc:
                    inc = diff
        #     while seq[index + 1] > seq[index]:
        #         diff += seq[index + n] - seq[index]
        #         index = index + 1
        #     if diff > inc:
        #         inc = diff
        #     index = index + 1
        # else:
        #     index = index + 1
    return inc

btc_data = [ 6729.44, 6690.88, 6526.36, 6359.98, 6475.89, 6258.74,
             6485.10, 6396.64, 6579.00, 6313.51, 6270.20, 6195.01,
             6253.67, 6313.90, 6233.10, 6139.99, 6546.45, 6282.50,
             6718.22, 6941.20, 7030.01, 7017.61, 7414.08, 7533.92,
             7603.99, 7725.43, 8170.01, 8216.74, 8235.70, 8188.00,
             7939.00, 8174.06 ]
btc_data.reverse()

assert abs(max_increase(tuple(btc_data))-589.45) < 1e-6
assert max_increase((1,2,3,2)) == 2
assert max_increase([3,-1,2,1]) == 3

基本上,我試圖“保持你的想法”。 首先,您不需要增加index Python 為您做到這一點。 其次,由於您的檢查應該只是“前向”,您可以將序列中的每個元素與所有后續元素進行比較。 我使用嵌套的 for 循環來做到這一點(替換你的 while 循環)。 外循環遍歷序列(索引 i = 1 --> n),內循環檢查當前元素之后的所有元素的差異(索引 j = i --> n)。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM