计算原子质量
Calculating atomic mass
作为 Python 的新手...我想设计一个程序来计算原子质量,给定这样的分子 'C14-H10-O'。
(我知道,已经有非常漂亮的程序了,但我想创建自己的程序并为自己感到自豪。)
我的代码快完成了,但是我发现了一个我无法解决的问题。
这是我的代码:
masas = {'H': 1.007825, 'C': 12.01, 'O': 15.9994,
'N': 14.0067, 'S': 31.972071, 'P': 30.973762}
elements_list = ['H','C','O', 'N', 'S','P']
def calcula_masa_atomica(molecula):
"""
Calcula la masa atomica de una molecula
"""
import re
masa = 0.0
#Primero creamos los
molecula = re.sub('[-]', '', molecula) #Remove "-"
molecula = (re.split('(\d+)',molecula))#split numbers and elements
for i, ch in enumerate(molecula):
if ch in elements_list: #If the element is in the elements_list,
element_mass = masas.get(ch)# Get the mass of this element
if molecula[i + 1].isdecimal(): #If the inmediately item is decimal
masa += element_mass * int(molecula[i + 1])# Add the mass of this element by the number of times that element is present. In each iteration, the masses of each element will be added and stored in the mass object
# If not, just add that element's mass to the sum.
else:
masa += element_mass
return masa
分子看起来像 'C14-H10-O2' 没问题,但是如果某些元素在元素后没有一个数字,Python 回复此错误。
IndexError: 列表索引超出范围
我知道我遇到了什么问题,但我不知道如何解决。
使用正则表达式 '(([A-Z])([0-9]*))'
你会找到分子中的所有基团,就像这样
'C14-H10-O2' > [('C14', 'C', '14'), ('H10', 'H', '10'), ('O2', 'O', '2')]
'H2O' > [('H2', 'H', '2'), ('O', 'O', '')]
然后遍历每个组,从 atom
中找到质量,然后乘以数字(如果没有,则 1
)
def calcula_masa_atomica(molecula):
masa = 0.0
for group, atom, nb in re.findall(r'(([A-Z])([0-9]*))', molecula):
element_mass = masas.get(atom, 0)
masa += element_mass * int(nb or 1)
return masa
使用列表理解语法,就是:
def calcula_masa_atomica(molecula):
return sum(masas.get(atom, 0) * int(nb or 1) for _, atom, nb in re.findall(r'(([A-Z])([0-9]*))', molecula))
作为 Python 的新手...我想设计一个程序来计算原子质量,给定这样的分子 'C14-H10-O'。
(我知道,已经有非常漂亮的程序了,但我想创建自己的程序并为自己感到自豪。)
我的代码快完成了,但是我发现了一个我无法解决的问题。
这是我的代码:
masas = {'H': 1.007825, 'C': 12.01, 'O': 15.9994,
'N': 14.0067, 'S': 31.972071, 'P': 30.973762}
elements_list = ['H','C','O', 'N', 'S','P']
def calcula_masa_atomica(molecula):
"""
Calcula la masa atomica de una molecula
"""
import re
masa = 0.0
#Primero creamos los
molecula = re.sub('[-]', '', molecula) #Remove "-"
molecula = (re.split('(\d+)',molecula))#split numbers and elements
for i, ch in enumerate(molecula):
if ch in elements_list: #If the element is in the elements_list,
element_mass = masas.get(ch)# Get the mass of this element
if molecula[i + 1].isdecimal(): #If the inmediately item is decimal
masa += element_mass * int(molecula[i + 1])# Add the mass of this element by the number of times that element is present. In each iteration, the masses of each element will be added and stored in the mass object
# If not, just add that element's mass to the sum.
else:
masa += element_mass
return masa
分子看起来像 'C14-H10-O2' 没问题,但是如果某些元素在元素后没有一个数字,Python 回复此错误。
IndexError: 列表索引超出范围
我知道我遇到了什么问题,但我不知道如何解决。
使用正则表达式 '(([A-Z])([0-9]*))'
你会找到分子中的所有基团,就像这样
'C14-H10-O2' > [('C14', 'C', '14'), ('H10', 'H', '10'), ('O2', 'O', '2')]
'H2O' > [('H2', 'H', '2'), ('O', 'O', '')]
然后遍历每个组,从 atom
中找到质量,然后乘以数字(如果没有,则 1
)
def calcula_masa_atomica(molecula):
masa = 0.0
for group, atom, nb in re.findall(r'(([A-Z])([0-9]*))', molecula):
element_mass = masas.get(atom, 0)
masa += element_mass * int(nb or 1)
return masa
使用列表理解语法,就是:
def calcula_masa_atomica(molecula):
return sum(masas.get(atom, 0) * int(nb or 1) for _, atom, nb in re.findall(r'(([A-Z])([0-9]*))', molecula))