-1

I am trying write a code for envaluation of postfix expression but I failed from the very beginning.

I put the postfix expression to a list and I am trying to split it like ["2","+","3","*","(","4","+","2",")"], but I could not do that. What am I doing wrong with that?

Here is the related code:

print(convertx(infix))
a=convertx(infix)
listtt=[]
listtt.append((a))
x=listtt[0]
for i in range(len(x)):
    for n in range
    li=[x[:-i]]

print(li)

This is not working clearly. I look at the code. a is a string which is consist of postfix expression and I converted it to a list.

EDIT:I tried to write this code:

print(convertx(infix))
a=convertx(infix)
listtt=[]
listtt.append((a))
x=listtt[0]
for i in range(len(x)):
    for n in range
    li=[x[:-i]]

But when I write this code I am just getting ['3'] as output but I want is getting them like:

["3","2","4","2","+",",""6","3","/","+","*"]

5
  • what exactly is the input infix and function convertx? Commented Feb 14, 2022 at 9:50
  • input is 3242+63/+*.I am using convertx for converting infix to postfix.@ Prats Commented Feb 14, 2022 at 9:53
  • 1
    You aren't converting a to a list, you merely add it to one. Please add the code for the convertx() function along with the original inputs and outputs. Commented Feb 14, 2022 at 9:55
  • Okay I am gonna add the code@ Jan Wilamowski Commented Feb 14, 2022 at 9:57
  • Does this answer your question? Break string into list of characters in Python Commented Feb 14, 2022 at 10:18

2 Answers 2

0

If you just want to turn a into a list of its single elements, simply run

a_list = [*a]
Sign up to request clarification or add additional context in comments.

Comments

0

If the output of the convertx()-function is a string, you can simply convert it to a list to get the expected result of getting a list of strings.

Example

a = "2+3*(4+2)"
converted = list(a)
print(converted)
["2","+","3","*","(","4","+","2",")"]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.