问题
I'm trying to build a RegEx to parse the coefficients of a polynomial from a string. I thought I'd found a solution until I found one particular example, which I suspect is badly formatted, that broke my RegEx. I'm also not sure my solution is the most elegant.
Here are some examples of the strings I need to parse:
polys = ['1x',
'3.8546E-27x^4-2.4333E-20x^3+5.1165E-14x^2+3.7718E-6x-6.1561E-1',
'6.13159E-3x+0.348',
'0.0100708x-40',
'6.103516E-3x',
'1E-6x',
'1.4846859E-6x',
'2435',
'2.7883E-27x^4-2.2164E-20x^3+5.8443E-14x^2+7.5773E-6x-1.3147E+']
And my attempted pattern and matching:
pattern = r'([\+\-]?\d+\.?\d+[Ee]?[\+\-]?\d*)[x\^\d+]?|([\+\-]?\d+\.?\d*[Ee]?[\+\-]?\d*)x'
for poly in polys:
coeffs = []
for match in re.finditer(pattern, poly):
groups = match.groups()
coeff = groups[0] if groups[0] is not None else groups[1]
coeffs.append(float(coeff))
print(coeffs)
This seems to work on all but the last poly in the list, which only fails on conversion to float. If I add the expected 0 to the end of this then the results are as follows, which is the result I'm looking for.
[1.0]
[3.8546e-27, -2.4333e-20, 5.1165e-14, 3.7718e-06, -0.61561]
[0.00613159, 0.348]
[0.0100708, -40.0]
[0.006103516]
[1e-06]
[1.4846859e-06]
[2435.0]
[2.7883e-27, -2.2164e-20, 5.8443e-14, 7.5773e-06, -1.3147]
I can probably assume that the last item is malformed and either ignore or handle it but I can't help but think there's a better/neater solution.
回答1:
The error comes from the last line from the last number -1.3147E+
. This is not a correct notation, the indice after the E
is missing.
One solution might be to replace it before applying your steps with:
poly = re.sub(r"(E\+)$", '', poly)
The code becomes:
pattern = r'([\+\-]?\d+\.?\d+[Ee]?[\+\-]?\d*)[x\^\d+]?|([\+\-]?\d+\.?\d*[Ee]?[\+\-]?\d*)x'
for poly in polys:
poly = re.sub(r"(E\+)$", '', poly)
coeffs = []
for match in re.finditer(pattern, poly):
groups = match.groups()
coeff = groups[0] if groups[0] is not None else groups[1]
coeffs.append(float(coeff))
print(coeffs)
# [1.0]
# [3.8546e-27, -2.4333e-20, 5.1165e-14, 3.7718e-06, -0.61561]
# [0.00613159, 0.348]
# [0.0100708, -40.0]
# [0.006103516]
# [1e-06]
# [1.4846859e-06]
# [2435.0]
# [2.7883e-27, -2.2164e-20, 5.8443e-14, 7.5773e-06, -1.3147]
Here is another way to do with split
:
out = []
for poly in polys:
poly = re.sub(r"(E\+)$", '', poly)
list_coef = re.split(r'x[\^\d]*', poly)
list_coef = [float(elt) for elt in list_coef if elt]
out.append(list_coef)
[print(o) for o in out]
# [1.0]
# [3.8546e-27, -2.4333e-20, 5.1165e-14, 3.7718e-06, -0.61561]
# [0.00613159, 0.348]
# [0.0100708, -40.0]
# [0.006103516]
# [1e-06]
# [1.4846859e-06]
# [2435.0]
# [2.7883e-27, -2.2164e-20, 5.8443e-14, 7.5773e-06, -1.3147]
来源:https://stackoverflow.com/questions/57439831/parsing-polynomial-coefficients-from-string