2017-03-11 1 views
0
그래서 여기 내 코드입니다

:파이썬 중위는 평가 AttributeError를 게시-해결하기 : '목록'객체에는 속성이 없습니다 '분할'

from StackClass import Stack 


def postfixEval(postfix): 
    os = Stack() 


tokenList = postfix.split() 

for token in tokenList: 
    if token in "": 
     os.push(int(token)) 
    else: 
     op2 = os.pop() 
     op1 = os.pop() 
     result = doMath(token,op1,op2) 
     os.push(result) 
return os.pop() 

def doMath(op, op1, op2): 
    if op == "*": 
     return op1 * op2 
    elif op == "/": 
    return op1/op2 
elif op == "+": 
    return op1 + op2 
else: 
    return op1 - op2 



def pres(p): 
    if p is '(': 
     return 0 
    elif p is '+' or '-': 
     return 1 
    elif p is '*' or '/': 
     return 2 
    else: 
     return 99 

def read(p): 
    if p is '(': 
     return left 
    elif p is ')': 
     return right 
    elif p is '+' or p is '-' or p is '*' or p is '%' or p is '/': 
     return operator 
    elif p is ' ': 
     return empty  
    else : 
     return operand       




def infixtopostfix(infixexp): 

    for i in infixexp : 
     type = read(i) 
     if type is left : 
      outlst.append(i) 
     elif type is right : 
      next = outlst.pop() 
      while next is not '(': 
       postfix.append(next) 
       next = outlst.pop() 
     elif type is operand: 
      postfix.append(i) 
    elif type is operator: 
     p = pres(i) 
     while len(outlst) is not 0 and p <= pres(outlst[-1]) : 
      postfix.append(outlst.pop()) 
     outlst.append(i) 
    elif type is empty: 
     continue 

while len(outlst) > 0 : 
    postfix.append(outlst.pop()) 


print "It's postfix notation is ",''.join(postfix) 

메인 프로그램

진정한 동안 :

postfix = [] 
outlst = [] 
operator = -10 
operand = -20 
left = -30 
right = -40 
empty = -50 


infixexp = raw_input("\nEnter the infix notation : ") 
infixtopostfix(infixexp) 
print(postfixEval(postfix)) 



choice = raw_input("\nDo you want to continue?<1-Yes/0-No>: ") 

if choice == '0': 
    break 

이 오류가 발생합니다 : AttributeError : 'list'객체에 'split'속성이 없습니다. 어디서 왔는지 모르겠지만 조인을 사용하여 목록의 문자열을 시도했지만 적용되지 않습니다. 나는 이것을 게시해야 할 것 같은 느낌이 들었고, 도움이 필요하며 스택 구현 및 평가에서도이 오류와 관련하여 문제가있는 사람들에게 도움이 필요합니다.

그건 그렇고, infixtopostfix (infixexp) 부분에서 어떻게 서로 공백으로 결과를 만들 수 있는지 알고 있습니까? 예 : 13 + 5 대신 * 1 3 + 5 *로 만듭니다. 어떻게해야합니까? 나는 지금 그것을 알아 내고있다. 나는 여기서 내 자신의 질문에 대답 할 것이다. 내가 문제를 해결하면. 그러나 시간 효율성을 위해 누군가가이 문제에 부딪쳐서 과거에 문제를 해결했을 수 있습니다. 제발 도와주세요 :) 고마워요! postfixEval tokenList = 후위에 인쇄에서 파일 "practice.py", 라인 (112), (postfixEval (후위)) 파일 "practice.py", 라인 8 :

역 추적

역 추적 (가장 최근 통화 최종) .split() AttributeError : 'list'객체에 'split'속성이 없습니다.

+0

전체 스택 트레이스를 게시하십시오 – e4c5

+0

전체 추적 표시 –

+0

목록 개체에 분할이없는 오류가 분명합니다. 'import pdb;를 추가하십시오. inflixxxxx의 형식이 무엇인지 모르는 경우 pdb.set_trace() '를 호출하십시오. – Shuo

답변

0

코드에서 postfix = []postfix.append(outlst.pop()) 은 후위가 목록임을 보여줍니다. 목록에는 split()과 같은 메소드가 없으며 str 오브젝트에 적용 할 수 있습니다.

+0

그럼 전에 postfix str을 만들어야합니까? 이거? str (후위) tokenList = postfix.split() 동일한 오류가 발생합니다. – Rekt

+0

들어요, split()의 목적은 무엇입니까? split()은 목록을 반환하고 이미 그 목록을 가지고 있습니다. 그렇지 않니? –

0

스 캐 이싱 문제에 대한 답변. raw_input 뒤에 중위 표현식을 나누기 만하면됩니다. 그런 식으로 100을 하나의 문자열로 인식합니다. 결과는 내가 내 게시물에 포함 된 것입니다. 당신이 몰랐을 때.

관련 문제