Parsing: Write a function which will take a string representing a chemical species and return a list of tuples consisting of elements and corresponding subscripts. In the absence of a subscript, the subscript should be 1. Example: calling your function with an input of:
H2SO4
should return an output of:
[('H', 2), ('S', 1), ('O', 4)]
So I'm trying to do a project but I'm not sure how to start it
can anyone help me how to start this?
The following takes you 90% of the way:
In [6]: re.findall(r'([A-Z][a-z]*)(\d*)', 'H2SO4')
Out[6]: [('H', '2'), ('S', ''), ('O', '4')]
The remaining 10% are left as an exercise for the reader (after all, this is homework).
Hint: a simple list comprehension can do the rest.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With