[英]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.