简体   繁体   English

为什么我的列表不会填充? IndexError:列出超出范围

[英]Why won't my list populate? IndexError: List Out of Range

Reading up on Stacks so I tried out this Infix to Postfix exercise (found here ). 阅读Stacks,所以我尝试了这个Infix to Postfix练习(在这里找到)。 You'll have to scroll a little bit to see their code. 你必须滚动一点才能看到他们的代码。 I tried to stay as true to their original implementation as possible. 我尽可能保持原始实现的真实性。

My code: http://pastebin.com/dG4Ku14n 我的代码: http//pastebin.com/dG4Ku14n

I'm getting an error on line 18 (where I define the peek variable). 我在第18行(我定义了peek变量)中收到错误。 It says the list is out of range but I shouldn't have called the list yet? 它说列表超出范围但我不应该调用列表吗? Shouldn't it just be stored in the variable and the actual error should occur later in the document when I use "prec[peek]" on line 49? 它不应该只存储在变量中,当我在第49行使用“prec [peek]”时,文档后面会出现实际错误吗?

I'm sure this code is more fubar than I realize. 我确信这段代码比我意识到的要多得多。 Any help would be appreciated. 任何帮助,将不胜感激。 Should I start over? 我应该重新开始吗?

Short version: 精简版:

peek = operator_stack[len(operator_stack)-1]
for element in infix:
    if:
        #code
    else:
    while not operator_stack and prec[peek] >= prec[element]:
        output_queue.append(operator_stack.pop())
    operator_stack.append(element)

Expected Output: 预期产出:

A B * C + D *

Unfortunately you operator_stack list is empty therefore it returns a IndexError BTW if you want to find the last element of a list use and make sure to make it to None if its empty: 不幸的是, operator_stack列表为空,因此如果要查找列表使用的最后一个元素,它会返回一个IndexError BTW,如果它为空,请确保将其设置为None

So use: 所以使用:

peek = operator_stack[-1] if operator_stack else None

instead of: 代替:

peek = operator_stack[len(operator_stack)-1]

Also when debugging your code it is clearly visible from the comments that these lines in: 此外,在调试代码时,从这些行中的注释中可以清楚地看到:

line 49 : while not operator_stack and prec[peek] >= prec[element]: 第49行while not operator_stack and prec[peek] >= prec[element]:

line 59 : while not operator_stack: 第59行while not operator_stack:

should actually look like: 应该看起来像:

line 49 : while operator_stack and prec[peek] >= prec[element]: 第49行while operator_stack and prec[peek] >= prec[element]:

line 59 : while operator_stack: 第59行while operator_stack:

Finally add a if statement to check if peek is None 最后添加一个if语句来检查peek是否为None

A short version would be 一个简短的版本

#line 18
peek = operator_stack[-1] if operator_stack else None
#line 49
if peek is not None:
    while operator_stack and prec[peek] >= prec[element]:

            #Append the pop'd element of the operator stack to the
            #output_queue list.
            output_queue.append(operator_stack.pop())

    #Append whatever is left (+,-,*,/) to the operator stack
    operator_stack.append(element)

    #line 59
    while operator_stack:
            #Append the last element in the stack until the stack is empty.
            output_queue.append(operator_stack.pop())

If you doubt what while operator_stack: means, see this simple example: 如果你怀疑 while operator_stack:是什么意思,请看这个简单的例子:

>>> a = [2,5,6]
>>> while a: # a is not empty right?
...     print 2 # so print 2
...     break # and break
2

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么我的索引超出范围? IndexError:列表索引超出范围 - Why is my index out of range? IndexError: list index out of range IndexError:列表索引超出范围为何? - IndexError: list index out of range Why? 为什么Django中/ list索引处的IndexError超出范围 - Why IndexError at / list index out of range in Django IndexError:列表索引超出范围,不确定原因 - IndexError: list index out of range, not sure why 页面不会停止加载python套接字+错误IndexError:列表索引超出范围 - Page won't stop loading python socket + error IndexError: list index out of range IndexError: list index out of range - 如果我的运动传感器没有检测到运动或检测到太多运动,为什么它会崩溃? - IndexError: list index out of range - Why does my movement sensor crash if it doesn't detect movement or it detects too much of it? 当我的代码似乎在范围内时,为什么会出现 IndexError: list index out of range? - Why am I getting an IndexError: list index out of range when my code appears to be within the range? 我不知道为什么会出现“IndexError: list index out of range” - I don't know why 'IndexError: list index out of range' came out IndexError:列出索引超出范围,如果 - IndexError: list index out of range in if IndexError:列表索引超出范围? - IndexError: list index out of range?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM