gpt4 book ai didi

python - 某些数据未存储在数组中

转载 作者:太空宇宙 更新时间:2023-11-03 20:27:07 25 4
gpt4 key购买 nike

所以我正在编写这段代码,它将化学方程式分解为化合物,然后将它们分解为元素及其原子数。但是,由于某种原因,每个化合物的最后一个元素没有存储在数组中。

class compound(object):  # A class of compounds. It stores all the relevant data for the compound
def __init__(self, n_compound):
self.n_compound = n_compound
self.f_compound = str(n_compound)
temp = ""
e = ""
v = "0"
self.element = []
self.val = []
for i in range(0, len(self.f_compound)-1):
if self.f_compound[i].isalpha() and self.f_compound[i+1].isupper():
temp += self.f_compound[i] + "1"
else:
temp += self.f_compound[i]
temp += self.f_compound[len(self.f_compound) - 1]
if temp[len(temp)-1].isalpha():
temp += "1"
self.f_compound = temp
########issue is appearing in the code here
for i in range(0, len(self.f_compound)):
if self.f_compound[i].isalpha():
if v != "0":
self.element.append(str(e))
self.val.append(int(v))
e = self.f_compound[i]
v = "0"
i -= 1
else:
e += self.f_compound[i]
elif self.f_compound[i].isdigit():
v += self.f_compound[i]
########This block of code Is not working properly
print(self.n_compound)
for x in range(0, len(self.element)):
print(self.element[x] + ": " + str(self.val[x]))
print(".............................................")



equation = str('Cu + HNO3 -> CuN2O6 + H2O + NO')
#str(input("Enter chemical equation: "))
equation = equation.replace("->", "+").replace(' ', '')
compounds = [] # An array of compounds

for i in range(0, len(equation.split("+"))):
# Assigning a compound name to an object of the compound class and storing it in the array
compounds.append(compound(equation.split("+")[i]))

我得到这个输出:


................................................
硝酸
高:1
数:1
................................................
氮化亚铜
铜:1
数:2
................................................

高:2
................................................

数:1
................................................

但是,正确的输出应该是这样的:


铜:1
................................................
硝酸
高:1
数:1
奥:3
................................................
氮化亚铜
铜:1
数:2
奥:6
................................................

高:2
奥:1
................................................

数:1
奥:1
................................................

最佳答案

如果最后一个元素未显示,这意味着您必须查看 for 循环是否排除了它,或者只是不处理上次迭代后的结果。

在这种情况下,我认为您只需在循环结束后附加最后一个元素及其价:

for i in range(0, len(self.f_compound)):
if self.f_compound[i].isalpha():
if v != "0":
self.element.append(str(e))
self.val.append(int(v))
e = self.f_compound[i]
v = "0"
i -= 1
else:
e += self.f_compound[i]
elif self.f_compound[i].isdigit():
v += self.f_compound[i]
# Fix :
self.element.append(str(e))
self.val.append(int(v))
<小时/>

结果:

Cu
Cu: 1
.............................................
HNO3
H: 1
N: 1
O: 3
.............................................
CuN2O6
Cu: 1
N: 2
O: 6
.............................................
H2O
H: 2
O: 1
.............................................
NO
N: 1
O: 1
.............................................

关于python - 某些数据未存储在数组中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57741705/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com