Python: chemical elements counter

放肆的年华 提交于 2021-02-05 12:29:07

问题


I want to get the elements for a given mixture. For examples, for a mixsture of Air (O2 and N2) and Hexane (C6H14) given by the dict with their respectives mole numbers

mix = {'O2': 1, 'N2': 3.76, 'C6H14': 0.01}

I want to get the following:

{O: 2, N: 7.52, C:0.06, H: 0.14}

Another example:

mix = {'C6H14': 1, 'C9H20': 1}

must yields

{H: 34, C: 15}
enter code here

The sequence of the dict it's not important. I was trying with the re.split, but I don't get any progress. If anyone can help me I will be grateful.

Edit: Hi, perhaps I wasn't clear in my question but what I want is to count the number of atoms in a mixture. I tryied to use the re.findall from the regular expressions library. I tried to separate the numbers from the another characters. Example:

mix  = {'C6H14': 1, 'C9H20': 1}
atmix = []
mix = {'O2': 1, 'N2': 3.76, 'C6H14': 0.01}
for x in mix.keys():
    tmp = re.findall(r'[A-Za-z]+|\d+', x)
    tmp = list(zip(tmp[0::2], tmp[1::2]))
    atmix.append(tmp)

for know i have:

>>> atmix
[(['O'], ['2']), (['N'], ['2']), (['C', 'H'], ['6', '14'])]

This is a list with tuples of the substances and their numbers of atoms. From here, I need to get each substance and relate with the number of atoms multiplied by the number of mols given by the mix dictionary, but I don't know how. The way I'm trying to separate the substances and their atoms from the mixture seems dumb. I need a better way to classify these substances and their atoms and discover how to relate it with the number of moles.

Thank in advance


回答1:


You can iterate over the mix dict while using a carefully-crafted regex to separate each element from its count.

import re
from collections import defaultdict

mix = {'O2': 1, 'N2': 3.76, 'C6H14': 0.01}
out = defaultdict(float)
regex = re.compile(r'([A-Z]+?)(\d+)?')

for formula, value in mix.items():
    for element, count in regex.findall(formula):
        count = int(count) if count else 1  # supporting elements with no count,
                                            # eg. O in H2O
        out[element] += count * value

print(out)

outputs

defaultdict(<class 'float'>, {'O': 2.0, 'N': 7.52, 'C': 0.06, 'H': 0.14})


来源:https://stackoverflow.com/questions/63983477/python-chemical-elements-counter

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!