Python Forum
Taking Mathematical Expressions from Strings
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Taking Mathematical Expressions from Strings
#1
I have bunch of strings like:

import re
import ast
import numpy as np

1/2 + 1/2*i + 1/2*j + 1/2*k
1 + 0*i + 0*j + 0*k
1 + 1*i + 0*j + 0*k 
etc...

I am trying to extract the numbers from these strings
s = ''.join(x for x in a if x.isdigit())
ele = (int(s[0]) ** 2 )+ (int(s[1]) ** 2) - (int(s[2]) **2) - (int(s[3]) ** 2)
that scripts work for second and third line and then I can simply extract the each of the number by specifying the index.
However for the strings like in the first line, I can't take 1/2 but I can take the number as 12 so my calculation comes as wrong ...
What I need is that take 1/2 as number and print it the whole thing as number like (0.5) or just as it is...
How can I do that?

Thanks in advance
Reply
#2
The lines that you want to parse are Python code, so use the built-in parsing capabilities of Python. You can parse with the ast module, or simply extract a sequence of tokens with tokenize.tokenize()

>>> from tokenize import tokenize                                                           
>>> import io                                                                               
>>> line = "1/2 + 1/2*i + 1/2*j + 1/2*k\n"                                                  
>>>                                                                                         
>>> for token in tokenize(io.BytesIO(line.encode()).readline):                              
...     print(token)                                                                        
...                                                                                         
TokenInfo(type=63 (ENCODING), string='utf-8', start=(0, 0), end=(0, 0), line='')            
TokenInfo(type=2 (NUMBER), string='1', start=(1, 0), end=(1, 1), line='1/2 + 1/2*i + 1/2*j +
 1/2*k\n')                                                                                  
TokenInfo(type=54 (OP), string='/', start=(1, 1), end=(1, 2), line='1/2 + 1/2*i + 1/2*j + 1/
2*k\n')                                                                                     
TokenInfo(type=2 (NUMBER), string='2', start=(1, 2), end=(1, 3), line='1/2 + 1/2*i + 1/2*j +
 1/2*k\n')                                                                                  
TokenInfo(type=54 (OP), string='+', start=(1, 4), end=(1, 5), line='1/2 + 1/2*i + 1/2*j + 1/
2*k\n')                                                                                     
TokenInfo(type=2 (NUMBER), string='1', start=(1, 6), end=(1, 7), line='1/2 + 1/2*i + 1/2*j + 1/2*k\n')
TokenInfo(type=54 (OP), string='/', start=(1, 7), end=(1, 8), line='1/2 + 1/2*i + 1/2*j + 1/
2*k\n')
TokenInfo(type=2 (NUMBER), string='2', start=(1, 8), end=(1, 9), line='1/2 + 1/2*i + 1/2*j + 1/2*k\n')
TokenInfo(type=54 (OP), string='*', start=(1, 9), end=(1, 10), line='1/2 + 1/2*i + 1/2*j + 1
/2*k\n')
TokenInfo(type=1 (NAME), string='i', start=(1, 10), end=(1, 11), line='1/2 + 1/2*i + 1/2*j +
 1/2*k\n')
TokenInfo(type=54 (OP), string='+', start=(1, 12), end=(1, 13), line='1/2 + 1/2*i + 1/2*j + 
1/2*k\n')
TokenInfo(type=2 (NUMBER), string='1', start=(1, 14), end=(1, 15), line='1/2 + 1/2*i + 1/2*j + 1/2*k\n')
TokenInfo(type=54 (OP), string='/', start=(1, 15), end=(1, 16), line='1/2 + 1/2*i + 1/2*j + 
1/2*k\n')
TokenInfo(type=2 (NUMBER), string='2', start=(1, 16), end=(1, 17), line='1/2 + 1/2*i + 1/2*j + 1/2*k\n')
TokenInfo(type=54 (OP), string='*', start=(1, 17), end=(1, 18), line='1/2 + 1/2*i + 1/2*j + 
1/2*k\n')
TokenInfo(type=1 (NAME), string='j', start=(1, 18), end=(1, 19), line='1/2 + 1/2*i + 1/2*j +
 1/2*k\n')
TokenInfo(type=54 (OP), string='+', start=(1, 20), end=(1, 21), line='1/2 + 1/2*i + 1/2*j + 
1/2*k\n')
TokenInfo(type=2 (NUMBER), string='1', start=(1, 22), end=(1, 23), line='1/2 + 1/2*i + 1/2*j
 + 1/2*k\n')
TokenInfo(type=54 (OP), string='/', start=(1, 23), end=(1, 24), line='1/2 + 1/2*i + 1/2*j + 
1/2*k\n')
TokenInfo(type=2 (NUMBER), string='2', start=(1, 24), end=(1, 25), line='1/2 + 1/2*i + 1/2*j
 + 1/2*k\n')
TokenInfo(type=54 (OP), string='*', start=(1, 25), end=(1, 26), line='1/2 + 1/2*i + 1/2*j + 
1/2*k\n')
TokenInfo(type=1 (NAME), string='k', start=(1, 26), end=(1, 27), line='1/2 + 1/2*i + 1/2*j +
 1/2*k\n')
TokenInfo(type=4 (NEWLINE), string='\n', start=(1, 27), end=(1, 28), line='1/2 + 1/2*i + 1/2
*j + 1/2*k\n')
TokenInfo(type=0 (ENDMARKER), string='', start=(2, 0), end=(2, 0), line='')
>>> 
Another solution, if your lines all have this exact structure that look like the computation of a quaternion would be to load a quaternion module, define the names i, j, k in the current namespace and simply evaluate the expression to get a quaternion.

The strange thing in these lines however is that a line such as 1/2 + 1/2*i + 1/2*j + 1/2*k in a Python program computes a result but then forgets it immediatly because it is not stored somewhere nor used in a call.
Reply
#3
If the format of the input changes, you may need some more elif clauses, otherwise, this works for the data you provided.

formulas = ['1/2 + 1/2*i + 1/2*j + 1/2*k', '1 + 0*i + 0*j + 0*k', '1 + 1*i + 0*j + 0*k']

def cleanup(alist):
    for i in range(len(alist)):
        newvalue = alist[i].strip()
        alist[i] = newvalue
    return alist
    
def workitout(astring):
    # astring may look like '1/2', '1/2*i', '1/2*j', or '1/2*k'
    thing = astring.split('*')    
    if len(thing) == 2 and not len(thing[0]) == 1:
        numerator = int(thing[0][0])
        denominator = int(thing[0][2])
        fraction = numerator / denominator
        return (fraction, thing[1])
    elif len(thing) == 2 and len(thing[0]) == 1:
        fraction = thing[0]
        return (fraction, thing[1])
    elif len(thing) == 1 and len(thing[0]) == 1:
        print('thing is just 1 long', thing[0])
        fraction = thing[0]        
        return (fraction)    
    elif len(thing) == 1 and len(thing[0]) == 3:
        numerator = int(thing[0][0])
        denominator = int(thing[0][2])
        fraction = numerator / denominator
        return (fraction)
  
for f in formulas:
    mylist = f.split('+')
    newlist = cleanup(mylist)
    print('newlist is', len(newlist), 'long')
    print('newlist looks like', newlist)
    for g in newlist:
        result = workitout(g)
        print('g is', g) 
        print('Result tuple =', result)
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  plotting based on the results of mathematical formulas Timur 1 351 Feb-08-2024, 07:22 PM
Last Post: Gribouillis
  Trying to understand strings and lists of strings Konstantin23 2 772 Aug-06-2023, 11:42 AM
Last Post: deanhystad
  Splitting strings in list of strings jesse68 3 1,779 Mar-02-2022, 05:15 PM
Last Post: DeaD_EyE
  Statements and Expressions Julie 1 1,639 Feb-26-2021, 05:19 PM
Last Post: nilamo
  Regular Expressions pprod 4 3,093 Nov-13-2020, 07:45 AM
Last Post: pprod
  About mathematical equations seyidcemkarakas 3 2,061 Oct-04-2020, 01:21 PM
Last Post: Gribouillis
  Recognising mathematical expressions from word and pdf file Preeti15 0 1,802 Aug-19-2020, 09:06 AM
Last Post: Preeti15
  Mathematical Conversion Scripts for Weather Station Mickey53usa 11 4,649 Jun-24-2020, 03:14 AM
Last Post: buran
  Mathematical Operators in String AgileAVS 1 2,401 Mar-04-2020, 04:14 PM
Last Post: Gribouillis
  Mathematical function input Tiiill 2 2,732 Sep-10-2019, 09:25 PM
Last Post: nilamo

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020