我正在尝试将元素从列表推送到堆栈。这是代码:
#!/usr/bin/python
class Stack :
def __init__(self) :
self.items = []
def push(self, item) :
self.items.append(item)
def pop(self) :
return self.items.pop()
def isEmpty(self) :
if self.items == []:
return true
def InsertIntostacks(lst1):
X = Stack() #for each expression a stack is defined
Y = Stack()
for words in lst1:
if (ord(words) >= 48 and ord(words) <= 57) or (ord(words) >=65 and ord(words) <= 90):
X.push(words)
else:
Y.push(words)
print X.pop()
if __name__ == '__main__':
a = open("testinput1.txt","r+")
wordList = [line.strip() for line in a];
#print wordList[1]
lst=list()
for words in wordList:
if words == '#':
print "End of file"
else:
lst = list(words)
lst1 = list()
print lst
for x1 in lst:
if x1 != ' ':
lst1.append(x1)
InsertIntostacks(lst1)
所以 X 被填充,我需要 Y 来包含运算符,但显然没有任何元素进入 Y (输入如 A=B=C,所以 Y 应该包含 = = )。如果我删除约束并将所有元素推送到一个堆栈中,则操作员就在那里。我在这里做错了什么?