简体   繁体   中英

Extract coefficients from mathematical linear expression in Python

Given a string containing a linear expression in some variables whose name might be not known, I would like to return a dictionary having the variable names as keys and the respective coefficients as values.

Example

Input:

s = "- x0 + 2 * x1 + 4 * x2 - 3 * y + z + 8"

Output:

{'x0': -1, 'x1': 2, 'x2': 4, 'y': -3, 'z': 1}

You could use a regex like ([+-])?\s*(?:(\d+)\s*\*\s*)?([az]\w*) . Here, the first part (the coefficient, including * ) is optional, but only the sign and the actual number will the memorized, as well as the variable. Then, You can convert those to a dict.

>>> import re
>>> s = "- x0 + 2 * x1 + 4 * x2 - 3 * y + z + 8"
>>> p = r"([+-])?\s*(?:(\d+)\s*\*\s*)?([a-z]\w*)"
>>> re.findall(p, s)
[('-', '', 'x0'),
 ('+', '2', 'x1'),
 ('+', '4', 'x2'),
 ('-', '3', 'y'),
 ('+', '', 'z')]
>>> {v: int(s+(c or '1')) for (s, c, v) in _ }
{'x0': -1, 'x1': 2, 'x2': 4, 'y': -3, 'z': 1}

Breakdown of the regex:

  • ([+-])?\s* : Optional sign, followed by spaces
  • (?:(\d+)\s*\*\s*)? Optional coefficient, followed by * and spaces; only the actual digits is captured
  • ([az]\w*) : Name of the variable

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM