英文:
How can i use the "for loop" for a chemical iteration?
问题
我遇到一个问题,我无法弄清楚如何编写我的循环代码。
n_masssection = 100
for i in range(n_masssection):
dn_New = dn_Sample(CO_in, H2O_in, H2_in, CO2_in)
n_New = dn_Pronew(CO_in, H2O_in, H2_in, CO2_in, dn_New)
dn_New1 = dn_Sample(n_New[0], n_New[1], n_New[2], n_New[3])
n_New1 = dn_Pronew(n_New[0], n_New[1], n_New[2], n_New[3], dn_New1)
"dn_Sample" 和 "dn_Pronew" 是函数。 "dn_Sample" 返回一个值,用于计算 "n_Pronew"。 "n_Pronew" 计算4个新的气体浓度,应该放在下一个 "section" 中,以计算一个新的 "dn_New",但是我不知道如何在不为每次计算编写新对象的情况下执行这个操作,因为那会花费很多时间。
有人可以帮我解决这个问题吗?
我有一个想法,可以在循环中保存结果,但是我的函数总是需要它们的4|5个参数来进行计算。如何用前一个函数的计算结果替代它们?
英文:
I have the problem that I can't figure out how to write my loop code.
n_masssection = 100
for i in range(n_masssection):
dn_New = dn_Sample(CO_in, H2O_in, H2_in, CO2_in)
n_New = dn_Pronew(CO_in, H2O_in, H2_in, CO2_in, dn_New)
dn_New1 = dn_Sample(n_New[0], n_New[1], n_New[2], n_New[3])
n_New1 = dn_Pronew(n_New[0], n_New[1], n_New[2], n_New[3], dn_New1)
"dn_Sample, dn_Pronew" are functions. "dn_Sample" returns a value that is used for the calculation of "n_Pronew". "n_Proneu" calculates 4 new gas concentrations which should be placed in the next "section" so calculate a ne dn_New with ne Sample function but now with the new gas concentrations. I want that my loop should do this 100 times... but I don't know how to do that without writing new objects for every calculation, which would take a lot of time.
Can someone help me with this?
I had the idea to save the results in the loop but my functions always need there 4|5 arguments to calculate. How to replace theme with the results of the calculation of the function before them.
答案1
得分: 1
n_masssection = 100
t = (CO_in, H2O_in, H2_in, CO2_in)
res = [t]
for i in range(n_masssection):
dn_New = dn_Sample(*res[-1])
n_New = n_Pronew(*res[-1], dn_New)
dn_New1 = dn_Sample(*n_New)
n_New1 = n_Pronew(*n_New, dn_New1)
res.append(n_New1)
然后,我认为res是你的最终结果。
英文:
n_masssection = 100
t = (CO_in, H2O_in, H2_in, CO2_in)
res = [t]
for i in range(n_masssection):
dn_New = dn_Sample(*res[-1])
n_New = n_Pronew(*res[-1], dn_New)
dn_New1 = dn_Sample(*n_New)
n_New1 = n_Pronew(*n_New, dn_New1)
res.append(n_New1)
Then, I think res is your final result.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论