【发布时间】:2017-08-07 04:06:48
【问题描述】:
我已经编写了这段代码,但我认为这不是最好的方法。
import re
notation = "41*(5.5+6-(8/2^3)-7)-1"
n2 = [x[0] for x in re.findall(r"(\d+(\.\d+)?)",notation)]
n2.reverse()
m = []
x = True
for z in notation:
if z.isdigit() and x:
m.append(n2.pop())
x = False
if not z.isdigit() and z != ".":
m.append(z)
x = True
预期的输出是:
m = ['41', '*', '(', '5.5', '+', '6', '-', '(', '8', '/', '2', '^', '3', ')', '-', '7', ')', '-', '1']
我认为这段代码有一种硬编码,我的疑问是如何以更好的方式制作它?
【问题讨论】:
标签: python regex list arithmetic-expressions