I am trying to split an arithmetic equation into a list of terms and operators
Question:
An examples of what im trying to do:
split('2.1 * -5 + 3 ^ 2 + 1 + -4.45')
should output
[2.1,'*',-5.0,'+',3.0,'^',2.0,'+',1.0,'+',-4.45]
my current code:
ops = ('+','-','*','/','^','(')
def split(txt):
final = []
temp = []
inp = "".join(txt.split())
for x in inp:
if x.isdigit():
temp.append(int(x))
elif x == '.':
temp.append(x)
elif x in ops:
if x == '-':
if len(temp) == 0:
if isinstance(final[-1],float):
final.append(float("".join(temp)))
final.append(x)
temp = []
else:
temp.append(x)
else:
final.append(float("".join(temp)))
final.append(x)
temp = []
else:
final.append("".join(temp))
final.append(x)
temp = []
final.append(float("".join(temp)))
print(final)
error, im not sure what im doing wrong here, and im not even sure if the code ive written is able to do what i want
Traceback (most recent call last):
File "C:UsersSahil PrustiDesktopcmpsc132HW 3hwtest.py", line 32, in <module>
print(split('2* ( -5.2 + 3 ) ^2+ ( 1 +4 )'))
File "C:UsersSahil PrustiDesktopcmpsc132HW 3hwtest.py", line 26, in split
final.append("".join(temp))
TypeError: sequence item 0: expected str instance, int found
Answers:
In line 32, you split the string.
print(split('2* ( -5.2 + 3 ) ^2+ ( 1 +4 )'))
But what you are appending in line 26 is int, not string therefore, you have an error.
final.append("".join(temp))
try
final.append("".join(str(temp)))
Don’t know exactly what is wrong with your program but you could reduce its complexity:
expr = '2.1 * -5 + 3 ^ 2 + 1 + -4.45'
ops = ('+','-','*','/','^','(')
terms = expr.split()
print([float(t) if t not in ops else t for t in terms])
#[2.1, '*', -5.0, '+', 3.0, '^', 2.0, '+', 1.0, '+', -4.45]
For more sophisticated expressions a regular expression approach could be helpful.
How about:
s= '2.1 * -5 + 3 ^ 2 + 1 + -4.45'
splitted = [x for x in s.split(' ') if x !=""]
result = []
for x in splitted:
try:
result.append(float(x))
except ValueError:
result.append(x)
print(result)
An examples of what im trying to do:
split('2.1 * -5 + 3 ^ 2 + 1 + -4.45')
should output
[2.1,'*',-5.0,'+',3.0,'^',2.0,'+',1.0,'+',-4.45]
my current code:
ops = ('+','-','*','/','^','(')
def split(txt):
final = []
temp = []
inp = "".join(txt.split())
for x in inp:
if x.isdigit():
temp.append(int(x))
elif x == '.':
temp.append(x)
elif x in ops:
if x == '-':
if len(temp) == 0:
if isinstance(final[-1],float):
final.append(float("".join(temp)))
final.append(x)
temp = []
else:
temp.append(x)
else:
final.append(float("".join(temp)))
final.append(x)
temp = []
else:
final.append("".join(temp))
final.append(x)
temp = []
final.append(float("".join(temp)))
print(final)
error, im not sure what im doing wrong here, and im not even sure if the code ive written is able to do what i want
Traceback (most recent call last):
File "C:UsersSahil PrustiDesktopcmpsc132HW 3hwtest.py", line 32, in <module>
print(split('2* ( -5.2 + 3 ) ^2+ ( 1 +4 )'))
File "C:UsersSahil PrustiDesktopcmpsc132HW 3hwtest.py", line 26, in split
final.append("".join(temp))
TypeError: sequence item 0: expected str instance, int found
In line 32, you split the string.
print(split('2* ( -5.2 + 3 ) ^2+ ( 1 +4 )'))
But what you are appending in line 26 is int, not string therefore, you have an error.
final.append("".join(temp))
try
final.append("".join(str(temp)))
Don’t know exactly what is wrong with your program but you could reduce its complexity:
expr = '2.1 * -5 + 3 ^ 2 + 1 + -4.45'
ops = ('+','-','*','/','^','(')
terms = expr.split()
print([float(t) if t not in ops else t for t in terms])
#[2.1, '*', -5.0, '+', 3.0, '^', 2.0, '+', 1.0, '+', -4.45]
For more sophisticated expressions a regular expression approach could be helpful.
How about:
s= '2.1 * -5 + 3 ^ 2 + 1 + -4.45'
splitted = [x for x in s.split(' ') if x !=""]
result = []
for x in splitted:
try:
result.append(float(x))
except ValueError:
result.append(x)
print(result)