-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","/","+","*"]

  • what exactly is the input infix and function convertx? – Prats Feb 14 '22 at 09:50
  • input is 3242+63/+*.I am using convertx for converting infix to postfix.@ Prats –  Feb 14 '22 at 09: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. – Jan Wilamowski Feb 14 '22 at 09:55
  • Okay I am gonna add the code@ Jan Wilamowski –  Feb 14 '22 at 09:57
  • Does this answer your question? [Break string into list of characters in Python](https://stackoverflow.com/questions/9833392/break-string-into-list-of-characters-in-python) – Tomerikoo Feb 14 '22 at 10:18

2 Answers2

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",")"]
Cerberus
  • 11
  • 1
0

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

a_list = [*a]
John Giorgio
  • 634
  • 3
  • 10