如何以更少的内存消耗有效地计算 python 中分子的分子量?
How to calculate molecular weight of a molecule in python efficiently with less memory consumption?
这是我使用字典中元素的预定义值计算分子量(H2O、NO2、CO2、HCOOH)的代码。
mass = {"H":1, "O":16, "C":12, "N":14}
def moleculeMass(moleculeName):
p = 0
total = 0
for i in moleculeName:
if i.isalpha():
p = i
total += mass[i]
else:
total = total - mass[p] + (mass[p] * int(i))
return total
compoundName = input("input molecule name: ")
print(moleculeMass(compoundName))
在上面的 else
语句中有一个额外的步骤,如果在字母后遇到数字,则从 existing 元素的质量中减去总和。我怎样才能取消这个额外的检查?
你的代码看起来已经相当快了,但如果你想摆脱你看到的额外代码,你可以:
def moleculeMass(moleculeName):
total = 0
for i in moleculeName:
if i.isalpha():
mass_p = mass[i]
total += mass_p
else:
total += mass_p * (int(i) -1)
return total
正如其他人所指出的,这将不支持多数字定义。
如果您想支持 Cl2O2
或 C4H10
这样的分子,那么您可以尝试正则表达式拆分,但它会比您现在拥有的慢:
mass = {
"H" : 1,
"O" : 16,
"C" : 12,
"N" : 14,
"Cl" : 35
}
re_pattern = re.compile(r"([A-Z][a-z]?|\s+)")
def moleculeMass2(moleculeName):
total = 0
for symbol in [symbol for symbol in re_pattern.split(moleculeName) if symbol]:
if symbol.isalpha():
prior_mass = mass[symbol]
total += prior_mass
else:
total += prior_mass * (int(symbol) - 1)
return total
这应该为您提供适当的权重:
print(moleculeMass("NO2"))
print(moleculeMass("C4H10")) # wrong answer
print(moleculeMass("Cl2O4")) # key error
print(moleculeMass2("NO2"))
print(moleculeMass2("C4H10"))
print(moleculeMass2("Cl2O4"))
这是我使用字典中元素的预定义值计算分子量(H2O、NO2、CO2、HCOOH)的代码。
mass = {"H":1, "O":16, "C":12, "N":14}
def moleculeMass(moleculeName):
p = 0
total = 0
for i in moleculeName:
if i.isalpha():
p = i
total += mass[i]
else:
total = total - mass[p] + (mass[p] * int(i))
return total
compoundName = input("input molecule name: ")
print(moleculeMass(compoundName))
在上面的 else
语句中有一个额外的步骤,如果在字母后遇到数字,则从 existing 元素的质量中减去总和。我怎样才能取消这个额外的检查?
你的代码看起来已经相当快了,但如果你想摆脱你看到的额外代码,你可以:
def moleculeMass(moleculeName):
total = 0
for i in moleculeName:
if i.isalpha():
mass_p = mass[i]
total += mass_p
else:
total += mass_p * (int(i) -1)
return total
正如其他人所指出的,这将不支持多数字定义。
如果您想支持 Cl2O2
或 C4H10
这样的分子,那么您可以尝试正则表达式拆分,但它会比您现在拥有的慢:
mass = {
"H" : 1,
"O" : 16,
"C" : 12,
"N" : 14,
"Cl" : 35
}
re_pattern = re.compile(r"([A-Z][a-z]?|\s+)")
def moleculeMass2(moleculeName):
total = 0
for symbol in [symbol for symbol in re_pattern.split(moleculeName) if symbol]:
if symbol.isalpha():
prior_mass = mass[symbol]
total += prior_mass
else:
total += prior_mass * (int(symbol) - 1)
return total
这应该为您提供适当的权重:
print(moleculeMass("NO2"))
print(moleculeMass("C4H10")) # wrong answer
print(moleculeMass("Cl2O4")) # key error
print(moleculeMass2("NO2"))
print(moleculeMass2("C4H10"))
print(moleculeMass2("Cl2O4"))