import re
weight = dict(
H=1.008,
O=16,
#...
)
def split(mo):
"split molecule formula 'H2O' to atoms [('H', '2'), ('O', '')]
atoms = re.findall('([A-Z][a-z]?)(\d*)', mo)
def mass(atoms):
return sum(weight[atom]*(count and int(count) or 1) for atom,count in atoms)
|