[Python] Splitting a mathematical string into a list while keeping everything in matching...

Discussão em 'Python' iniciado por Stack, Junho 1, 2025.

  1. Stack

    Stack Membro Participativo

    Problem


    I am trying to get any mathematical string to split into a list by operators (i.e. "+", "-", "/", "*"), while keeping anything in a matching number of brackets together as one list element.

    Examples and desired outputs


    Here are some very random examples and the desired outputs of what I want to achieve:

    import math

    equation = "5+5*10"
    equation_segmented = ["5", "+", "5", "*", "10"]

    equation = "(2*2)-5*(math.sqrt(9)+2)"
    equation_segmented = ["(2*2)", "-", "5", "*", "(math.sqrt(9)+2)"]

    equation = "(((5-3)/2)*0.5)+((2*2))*(((math.log(5)+2)-2))"
    equation_segmented = ["(((5-3)/2)*0.5)", "+", "((2*2))", "*", "(((math.log(5)+2)-2))"]


    Note: alphabetical letters (or symbols like "π") should be included in the brackets too

    Finding a solution


    My first thought was using a regex :

    import re

    equation_segmented = re.split("([\+|\-|\*|\/]|\(.*\))", equation)


    The problem here, however, is that it does not account for matching brackets.

    I then thought of iterating through the string manually and keeping track of the parentheses with a counter, but did not get it to work (I was pretty much only able to write my own 're.split'-function).

    Lastly I went back to regex (equation_segmented = re.split("([\+|\-|\*|\/])", equation)) and thought about just splitting the string by operators, to then "".join() all the list elements in matching brackets afterwards - yet again to no avail.

    I am not sure if this might be a problem for a parser, but I do not have any experience with that at all, so I am not sure where to start.

    Help would be much appreciated - thank you!

    Continue reading...

Compartilhe esta Página