Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/jsf/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python:I';我误解了堆栈逻辑,但我';我不知道在哪里_Python_Stack - Fatal编程技术网

Python:I';我误解了堆栈逻辑,但我';我不知道在哪里

Python:I';我误解了堆栈逻辑,但我';我不知道在哪里,python,stack,Python,Stack,我有一个函数,它通过堆栈计算中缀表达式。(我打赌你以前从未见过这个。)它采用列表格式的表达式,其中列表中的每个项都是单个字符(操作数、运算符或括号) 我使用“tempvar”字符串的目的是,因为我正在逐个字符地迭代表达式,所以我将遇到一个关于多位数的问题。所以我把它们做成一条线,然后把线推到堆栈上 我有两个问题,我不知道为什么我会得到其中一个 输入 ((21*2)-(4/2)) 我得到输出 Pushing ( onto the stack Pushing ( onto the stack Pu

我有一个函数,它通过堆栈计算中缀表达式。(我打赌你以前从未见过这个。)它采用列表格式的表达式,其中列表中的每个项都是单个字符(操作数、运算符或括号)

我使用“tempvar”字符串的目的是,因为我正在逐个字符地迭代表达式,所以我将遇到一个关于多位数的问题。所以我把它们做成一条线,然后把线推到堆栈上

我有两个问题,我不知道为什么我会得到其中一个

输入

((21*2)-(4/2))
我得到输出

Pushing ( onto the stack
Pushing ( onto the stack
Pushing 21 onto the stack
Pushing * onto the stack
Pushing 2 onto the stack
Popping 2 from the stack
**Popping * from the stack
Popping 2 from the stack
Popping 1 from the stack
Popping 21 from the stack**
我用星号标出了问题部分。我对堆栈的理解是,它们的功能是后进先出,它们不在乎顶部是什么,pop将其取下。21是我推到堆栈上的第一件东西,但它看起来像是被单独读取(作为21,最后弹出的条目),并分成两半为“2”和“1”-但我希望1会在2之前弹出!我对stacks或我写的东西有什么误解

for x in popped:
    print 'Popping',x,'from the stack.'

popped
是一个字符串,因此此循环将一次迭代一个字符。

什么是
inStack
?你没有定义它。当我在这里键入这个时,我的命名不一致,我现在已经修复了。嗯。。。谈论在错误的地方寻找问题。非常感谢。
for x in popped:
    print 'Popping',x,'from the stack.'