Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Parsing chemical formula [closed]

Tags:

python

parsing

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?

like image 569
user1691827 Avatar asked Dec 26 '22 12:12

user1691827


1 Answers

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.

like image 65
NPE Avatar answered Dec 29 '22 00:12

NPE