Search code examples
pythonpython-3.xpython-2.7parsingparentheses

How to find the parentheses and calculate the percentage of elements in a chemical composition?


Hi I am trying to get the percentage of each element from a chemical composition. The following code works well for normal chemical compositions.

comp = "Ag20Al25La55"

re.findall('([A-Z][a-z]?)([0-9]*[.]?[0-9]*)', comp)

output is

[('Ag', '20'), ('Al', '25'), ('La', '55')]

Bur how can I get something similar with the parentheses?

comp = "(Cu60Zr40)98Y2"

The above code would give

[('Cu', '60'), ('Zr', '40'), ('Y', '2')]

But the correct output should be

[('Cu', '58.8'), ('Zr', '39.2'), ('Y', '2')]

Because we have to multiply 98 by 60% to get the percentage for Cu and multiply 98 by 40% to get the percentage for Zr.


Solution

  • You could try:

    def compute(x):
      y = float(x.group(2))
      return ''.join([i+str(float(j)*y/100) for i,j in re.findall('([A-z]+)(\\d+[.]?\\d*)', x.group(1))])
      
    def final(x): 
        cmp = re.sub('(\\(.*\\))(\\d+)', compute, x)
        return re.findall('([A-Z][a-z]?)([0-9]*[.]?[0-9]*)', cmp)
    
    final(comp)
    [('Cu', '58.8'), ('Zr', '39.2'), ('Y', '2')]
    

    Disclaimer: Not the best way to do this