Splitting a math expression string into tokens in Python

后端 未结 2 1138
死守一世寂寞
死守一世寂寞 2020-12-20 19:05

I have a lot of python strings such as \"A7*4\", \"Z3+8\", \"B6 / 11\", and I want to split these strings so that they would be in a l

相关标签:
2条回答
  • 2020-12-20 19:30

    You should split on the character set [+-/*] after removing the whitespace from the string:

    >>> import re
    >>> def mysplit(mystr):
    ...     return re.split("([+-/*])", mystr.replace(" ", ""))
    ...
    >>> mysplit("A7*4")
    ['A7', '*', '4']
    >>> mysplit("Z3+8")
    ['Z3', '+', '8']
    >>> mysplit("B6 / 11")
    ['B6', '/', '11']
    >>>
    
    0 讨论(0)
  • 2020-12-20 19:31

    There is a way to solve this without regular expressions by using the Python tokenizer. I used a more complex formula to show the capabilities of this solution.

    from io import StringIO
    import tokenize
    
    formula = "(A7*4) - (Z3+8) -  ( B6 / 11)"
    print([token[1] for token in tokenize.generate_tokens(StringIO(formula).readline) if token[1]])
    

    Result:

    ['(', 'A7', '*', '4', ')', '-', '(', 'Z3', '+', '8', ')', '-', '(', 'B6', '/', '11', ')']
    
    0 讨论(0)
提交回复
热议问题