从字符串解析多项式系数 [英] Parsing polynomial coefficients from string

查看:57
本文介绍了从字符串解析多项式系数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试构建一个 RegEx 来解析字符串中多项式的系数.我以为我已经找到了一个解决方案,直到我找到了一个我怀疑格式错误的特定示例,它破坏了我的 RegEx.我也不确定我的解决方案是最优雅的.

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)

这似乎适用于列表中除最后一个多边形之外的所有多边形,但仅在转换为浮动时失败.如果我将预期的 0 添加到此末尾,则结果如下,这就是我要查找的结果.

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.3147E+ 的最后一行.这不是一个正确的表示法,缺少 E 后面的索引.

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)

代码变成:

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]

这是另一种处理split的方法:

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]

这篇关于从字符串解析多项式系数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆