繁体   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