import re
# define the problem
mystring='x1 +3 x2 +2 x3 -2.2 x4 +19 x5'
# get coefficients
regex_coeff='([+-]\d*\.{0,1}\d+) x'
# assuming your polynome is normalized, we can add the one in front
coeffs=[1.0] + [float(x) for x in re.findall(regex_coeff,mystring)]
# get exponents
regex_expo='x(\d+)'
exponents=[int(x) for x in re.findall(regex_expo,mystring)]
# print results
print(coeffs)
print(exponents)
>>[1.0, 3.0, 2.0, -2.2, 19.0]
>>[1, 2, 3, 4, 5]
相关问题 更多 >
编程相关推荐