https://github.com/gitcordier/thechemicalbrowsers
Given a chemical formula, find how many H, He, Li, Be, B, C, N, ... are involved. Iterative algorithm, not the naive one!
https://github.com/gitcordier/thechemicalbrowsers
chemical-formula iterative-algorithms parser
Last synced: 2 months ago
JSON representation
Given a chemical formula, find how many H, He, Li, Be, B, C, N, ... are involved. Iterative algorithm, not the naive one!
- Host: GitHub
- URL: https://github.com/gitcordier/thechemicalbrowsers
- Owner: gitcordier
- License: mit
- Created: 2018-07-02T14:26:19.000Z (almost 7 years ago)
- Default Branch: master
- Last Pushed: 2020-07-23T20:11:41.000Z (almost 5 years ago)
- Last Synced: 2025-04-12T06:58:24.395Z (2 months ago)
- Topics: chemical-formula, iterative-algorithms, parser
- Language: Jupyter Notebook
- Homepage:
- Size: 29.3 KB
- Stars: 0
- Watchers: 0
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Contributing: CONTRIBUTING.md
- License: LICENSE
- Code of conduct: CODE_OF_CONDUCT.md
Awesome Lists containing this project
README
# A chemical formula parser
For a given chemical formula represented by a string, parser counts the number
of atoms of each element contained in the molecule and return a dict.For example:
```py
water = 'H2O'
parse_molecule(water) # Returns {'H': 2, 'O': 1}.magnesium_hydroxide = 'Mg(OH)2'
parse_molecule(magnesium_hydroxide)# Returns {'Mg': 1, 'O': 2, 'H': 2}.fremy_salt = 'K4[ON(SO3)2]2'
parse_molecule(fremy_salt) # Returns {'K': 4, 'O': 14, 'N': 2, 'S': 4}.
```
As you can see, some formulas have brackets in them.
The index outside the brackets tells you that you have to multiply count of
each atom inside the bracket on this index.For example, in Fe(NO3)2 you have one iron atom, two nitrogen atoms and six
oxygen atoms.Note that brackets may be round, square or curly and can also be nested.
Index after the braces is optional.# How to use the parser.
Simply run "python parser.py" (must be a python 3).
Alternatively, you can run "jupyter notebook", so that Parser.ipynb gets run.