Python 无限 while 和 for 循环
我的代码似乎陷入了无限循环。它应该遍历一条能量曲线,其中 2 个局部最小值之间的每个点都被添加到子列表 teil_reaction
中,然后移动到下一组点。此外,前一个子列表的最后一个点也是下一个子列表中的第一个点。
def reaction_components(reaction):
'''
reaction : List of processed frames which are extracted from trajectory
'''
results = []
i = 0
while i < len(reaction):
j = 0
teil_reaction = []
teil_reaction.append(reaction[i])
for m in range(i, len(reaction)-1):
if float(reaction[m+1][1]) > float(reaction[m][1]):
teil_reaction.append(reaction[m+1])
j = m + 1
for k in range(j, len(reaction)-1):
if float(reaction[k+1][1]) < float(reaction[k][1]):
teil_reaction.append(reaction[k+1])
i = k + 1
results.append(teil_reaction)
return results
reaction
的示例列表为 [['CCCCCCCCCCCCCSSc1nnc(s1)SSCCCCCCCCCCCC', '-103.306546877671'], ['CCCCCCCCCCCCCSSc1nnc(s1)SSCCCCCC[CH2].[CH2]CCCC', '-102.624657924951'], ['CCCCCCCCCCCCCSSc1nnc(s1)SSCCCCCCCCCCCC', '-102.684704435085'], ['CCCCCCCCCCCCCSS[C]1[N][N][C](S1)S[S].[CH2]CCCCCCCCCCCC' , '-102.672296611091'], ['CCCCCCCCCCCCCSS/[C]=N/[N]C(=S)SSCCCCCCCCCCCC', '-102.626944643619'], ['CCCCCCCCCCCSSc1nnc(s1)SSCCCCCCCCCCCC', '-102.733656248752'], ['CCCCCCCCCCCCCSS[C][N][N]C(=S)SSCCCCCCCCCCCC', '-102.705294168498'], ['CCCCCCCCCCCCCSSC1=N[N][C](S1)SSCCCCCCCCCCCC', '-102.670037250294'], ['CCCCCCCCCCCCCSS[C]1[N][N]/C(=[S]/SCCCCCCCCCCCC)/S1', '-102.729557090939'], ['CCCCCCCCCCCCCSSc1nnc(s1)SSCCCCCCCCCCCC', '-102.784465082168'], ['CCCCCCCCCCCCCSS/[C]=N/[N]C(=S)SSCCCCCCCCCCCC', '-102.678559581466'], ['CCCCCCCCCCCCCSSc1nnc(s1)SSCCCCCCCCCCCC', '-102.780173450946'], ['CCCCCCCCCCCCSS[C]1[N][N]/C(=[S]/SCCCCCCCCCCCC)/S1', '-102.706003472069'], ['CCCCCCCCCCCCCSS[C]1[N][N][C](S1)SSCCCCCCCCCCCC', '-102.694715899746'], ['CCCCCCCCCCCCCSS/[C]=N/[N]C(=S)SSCCCCCCCCCCCC', ' -102.701110428064'], ['CCCCCCCCCCCCCSS[C]1[N][N][C](S1)SSCCCCCCCCCCCC', '-102.711452078891'], ['CCCCCCCCCCCCCSSC(=S)[N]/N=[C]/SSCCCCCCCCCCCCCC', '- 102.753669258961'], ['CCCCCCCCCCCCCSS[C]1[N]N=C(S1)SSCCCCCCCCCCCC', '-102.636410618884'], ['CCCCCCCCCCCCCSSC(=S)[N]/N=[C]/SSCCCCCCCCCCCC', '-102.644213340892'] , ['CCCCCCCCCCCCCSS[C]1[N][N][C](S1)SSCCCCCCCCCCCC', '-102.741411107096'], ['CCCCCCCCCCCCCSSC(=S)[N][N][C]SSCCCCCCCCCCCCCC', '-102.779722244847 '], ['CCCCCCCCCCCCCSSc1nnc(s1)SSCCCCCCCCCCCC', '-102.726065146631'], ['CCCCCCCCCCCC[CH2].[S]SC1=N[N][C](S1)SSCCCCCCCCCCCC', '-102.704259648046'], ['CCCCCCCCCCCCCSS[C]1[N][N][C](S1)SSCCCCCCCCCCCC', '-102.672777278714'], ['CCCCCCCCCCCC[CH2].[S]S[C]1[N][N][ C](S1)SSCCCCCCCCCCCC', '-102.744655804617'], ['CCCCCCCCCCCC[S].S=C1N=N[C](S1)SSCCCCCCCCCCCC', '-102.669981592196'], ['CCCCCCCCCCCCCSS[C]1[N][N][C]( S1)SSCCCCCCCCCCCC', '-102.713068529744'], ['CCCCCCCCCCCCCSSC1=N[N][C](S1)S[S].[CH2]CCCCCCCCCCCC', '-102.645422980799'], ['CCCCCCCCCCCCCSS[C]1[N][N][C](S1)SSCCCCCCCCCCCC', '-102.729184657256'], ['CCCCCCCCCCCC[S].[S][C]1[N][N][C ](S1)SSCCCCCCCCCCCC', '-102.64224131805'], ['CCCCCCCCCCCCCSS[C]1[N][N][C](S1)SSCCCCCCCCCCCC', '-102.711487535061'], ['CCCCCCCCCCCSSC1=N[N]C(=S)S1.[ S]CCCCCCCCCCCC', '-102.715369662393'], ['CCCCCCCCCCCCSS[C]1[N][N][C](S1)SSCCCCCCCCCCCC', '-102.736595850749'], ['CCCCCCCCCCCC[S@]1S[C@@]21[N][N][C](S2)SSCCCCCCCCCCCC', '-102.672769568432'], ['CCCCCCCCCCCCCSS[C]1[N][N][C ](S1)SSCCCCCCCCCCCC', '-102.764725479807'], ['CCCCCCCCCCCCCSSC1=N[N]/C(=[S]\\SCCCCCCCCCCCC)/S1', '-102.695966797633'], ['CCCCCCCCCCCCCSS[C]1[N][N][C ](S1)SSCCCCCCCCCCCC', '-102.626868480522'], ['CCCCCCCCCCCC[CH2].[S]Sc1nnc(s1)SSCCCCCCCCCCCC', '-102.588700582455'], ['CCCCCCCCCCCCCSS[C]1[N][N][C](S1)SSCCCCCCCCCCCC' , '-102.674540493719'], ['CCCCCCCCCCCC[S@@]1S[C]2[N][N][C]([S@@]12)SSCCCCCCCCCCCC', '-102.643461645226'], ['CCCCCCCCCCCCCSS[C]1[N][N][C](S1)SSCCCCCCCCCCCC', '-102.752445118235'], ['CCCCCCCCCCCC[CH2].[S].S=C1[N][N][C ](S1)SSCCCCCCCCCCCC', '-102.708718648469'], ['CCCCCCCCCCCCCSS[C]1[N][N][C](S1)SSCCCCCCCCCCCC', '-102.730896209081'], ['CCCCCCCCCCCCCSS[C]1[N][N]C(= S)S1.[S]CCCCCCCCCCCC', '-102.719973340574'], ['CCCCCCCCCCCCCSS[C]1[N][N][C](S1)SSCCCCCCCCCCCC', '-102.665803332006'], ['CCCCCCCCCCCCCSSC(=S)[N][N][C] SSCCCCCCCCCCCC', '-102.754915967134'],['CCCCCCCCCCCCCSS[C]1[N][N][C](S1)SSCCCCCCCCCCCC','-102.744766028688'],['CCCCCCCCCCCCCSSC(=S)/N=N\\[C] SSCCCCCCCCCCCC', '-102.721441123337'], ['CCCCCCCCCCCCCSS[C]1[N]N=C(S1)SSCCCCCCCCCCCC', '-102.706423668641'], ['CCCCCCCCCCCCCSSC1=N[N]C(=S)S1.[S]CCCCCCCCCCCC ', '-102.747038763646'], ['CCCCCCCCCCCCCSS[C]1N=N[C](S1)SSCCCCCCCCCCCC', '-102.716391644738'], ['CCCCCCCCCCCCCSSc1nnc(s1)S[S].[CH2]CCCCCCCCCCCC', '-102.680511792892'],['CCCCCCCCCCCCCSS[C]1[N]N=C(S1)SSCCCCCCCCCCCC','-102.703678629573'],['CCCCCCCCCCCC[S]。[S][C]/N=N\\ C(=S)SSCCCCCCCCCCCC', '-102.655618420991'],['CCCCCCCCCCCCCSS[C]1[N][N][C](S1)SSCCCCCCCCCCCC','-102.714449469878'],['CCCCCCCCCCCCCSS[C]/N=N\\C(=S )SSCCCCCCCCCCCC', '-102.676486450894'], ['CCCCCCCCCCCCCSS[C]1[N][N][C](S1)SSCCCCCCCCCCCC', '-102.702312172869'], ['CCCCCCCCCCCCCSS[C][N][N]C(=S )SSCCCCCCCCCCCC', '-102.650883426888'], ['CCCCCCCCCCCCCSS[C][N]N=C=S.[S]SCCCCCCCCCCCC', '-102.673888660336'], ['CCCCCCCCCCCC[S].S=C=NN=C=SS= [S]CCCCCCCCCCCC', '-102.632130520839']]
My code seems to run into an infinite loop. It's supposed to traverse an energy curve where every points between 2 local minima are added to a sublist teil_reaction
and then it moves on to the next set of points. Furthermore, the last point of a previous sublist is also the first point in the next sublist.
def reaction_components(reaction):
'''
reaction : List of processed frames which are extracted from trajectory
'''
results = []
i = 0
while i < len(reaction):
j = 0
teil_reaction = []
teil_reaction.append(reaction[i])
for m in range(i, len(reaction)-1):
if float(reaction[m+1][1]) > float(reaction[m][1]):
teil_reaction.append(reaction[m+1])
j = m + 1
for k in range(j, len(reaction)-1):
if float(reaction[k+1][1]) < float(reaction[k][1]):
teil_reaction.append(reaction[k+1])
i = k + 1
results.append(teil_reaction)
return results
An example list for reaction
is [['CCCCCCCCCCCCSSc1nnc(s1)SSCCCCCCCCCCCC', '-103.306546877671'], ['CCCCCCCCCCCCSSc1nnc(s1)SSCCCCCC[CH2].[CH2]CCCC', '-102.624657924951'], ['CCCCCCCCCCCCSSc1nnc(s1)SSCCCCCCCCCCCC', '-102.684704435085'], ['CCCCCCCCCCCCSS[C]1[N][N][C](S1)S[S].[CH2]CCCCCCCCCCC', '-102.672296611091'], ['CCCCCCCCCCCCSS/[C]=N/[N]C(=S)SSCCCCCCCCCCCC', '-102.626944643619'], ['CCCCCCCCCCCCSSc1nnc(s1)SSCCCCCCCCCCCC', '-102.733656248752'], ['CCCCCCCCCCCCSS[C][N][N]C(=S)SSCCCCCCCCCCCC', '-102.705294168498'], ['CCCCCCCCCCCCSSC1=N[N][C](S1)SSCCCCCCCCCCCC', '-102.670037250294'], ['CCCCCCCCCCCCSS[C]1[N][N]/C(=[S]/SCCCCCCCCCCCC)/S1', '-102.729557090939'], ['CCCCCCCCCCCCSSc1nnc(s1)SSCCCCCCCCCCCC', '-102.784465082168'], ['CCCCCCCCCCCCSS/[C]=N/[N]C(=S)SSCCCCCCCCCCCC', '-102.678559581466'], ['CCCCCCCCCCCCSSc1nnc(s1)SSCCCCCCCCCCCC', '-102.780173450946'], ['CCCCCCCCCCCCSS[C]1[N][N]/C(=[S]/SCCCCCCCCCCCC)/S1', '-102.706003472069'], ['CCCCCCCCCCCCSS[C]1[N][N][C](S1)SSCCCCCCCCCCCC', '-102.694715899746'], ['CCCCCCCCCCCCSS/[C]=N/[N]C(=S)SSCCCCCCCCCCCC', '-102.701110428064'], ['CCCCCCCCCCCCSS[C]1[N][N][C](S1)SSCCCCCCCCCCCC', '-102.711452078891'], ['CCCCCCCCCCCCSSC(=S)[N]/N=[C]/SSCCCCCCCCCCCC', '-102.753669258961'], ['CCCCCCCCCCCCSS[C]1[N]N=C(S1)SSCCCCCCCCCCCC', '-102.636410618884'], ['CCCCCCCCCCCCSSC(=S)[N]/N=[C]/SSCCCCCCCCCCCC', '-102.644213340892'], ['CCCCCCCCCCCCSS[C]1[N][N][C](S1)SSCCCCCCCCCCCC', '-102.741411107096'], ['CCCCCCCCCCCCSSC(=S)[N][N][C]SSCCCCCCCCCCCC', '-102.779722244847'], ['CCCCCCCCCCCCSSc1nnc(s1)SSCCCCCCCCCCCC', '-102.726065146631'], ['CCCCCCCCCCC[CH2].[S]SC1=N[N][C](S1)SSCCCCCCCCCCCC', '-102.704259648046'], ['CCCCCCCCCCCCSS[C]1[N][N][C](S1)SSCCCCCCCCCCCC', '-102.672777278714'], ['CCCCCCCCCCC[CH2].[S]S[C]1[N][N][C](S1)SSCCCCCCCCCCCC', '-102.744655804617'], ['CCCCCCCCCCCC[S].S=C1N=N[C](S1)SSCCCCCCCCCCCC', '-102.669981592196'], ['CCCCCCCCCCCCSS[C]1[N][N][C](S1)SSCCCCCCCCCCCC', '-102.713068529744'], ['CCCCCCCCCCCCSSC1=N[N][C](S1)S[S].[CH2]CCCCCCCCCCC', '-102.645422980799'], ['CCCCCCCCCCCCSS[C]1[N][N][C](S1)SSCCCCCCCCCCCC', '-102.729184657256'], ['CCCCCCCCCCCC[S].[S][C]1[N][N][C](S1)SSCCCCCCCCCCCC', '-102.64224131805'], ['CCCCCCCCCCCCSS[C]1[N][N][C](S1)SSCCCCCCCCCCCC', '-102.711487535061'], ['CCCCCCCCCCCCSSC1=N[N]C(=S)S1.[S]CCCCCCCCCCCC', '-102.715369662393'], ['CCCCCCCCCCCCSS[C]1[N][N][C](S1)SSCCCCCCCCCCCC', '-102.736595850749'], ['CCCCCCCCCCCC[S@]1S[C@@]21[N][N][C](S2)SSCCCCCCCCCCCC', '-102.672769568432'], ['CCCCCCCCCCCCSS[C]1[N][N][C](S1)SSCCCCCCCCCCCC', '-102.764725479807'], ['CCCCCCCCCCCCSSC1=N[N]/C(=[S]\\SCCCCCCCCCCCC)/S1', '-102.695966797633'], ['CCCCCCCCCCCCSS[C]1[N][N][C](S1)SSCCCCCCCCCCCC', '-102.626868480522'], ['CCCCCCCCCCC[CH2].[S]Sc1nnc(s1)SSCCCCCCCCCCCC', '-102.588700582455'], ['CCCCCCCCCCCCSS[C]1[N][N][C](S1)SSCCCCCCCCCCCC', '-102.674540493719'], ['CCCCCCCCCCCC[S@@]1S[C]2[N][N][C]([S@@]12)SSCCCCCCCCCCCC', '-102.643461645226'], ['CCCCCCCCCCCCSS[C]1[N][N][C](S1)SSCCCCCCCCCCCC', '-102.752445118235'], ['CCCCCCCCCCC[CH2].[S].S=C1[N][N][C](S1)SSCCCCCCCCCCCC', '-102.708718648469'], ['CCCCCCCCCCCCSS[C]1[N][N][C](S1)SSCCCCCCCCCCCC', '-102.730896209081'], ['CCCCCCCCCCCCSS[C]1[N][N]C(=S)S1.[S]CCCCCCCCCCCC', '-102.719973340574'], ['CCCCCCCCCCCCSS[C]1[N][N][C](S1)SSCCCCCCCCCCCC', '-102.665803332006'], ['CCCCCCCCCCCCSSC(=S)[N][N][C]SSCCCCCCCCCCCC', '-102.754915967134'], ['CCCCCCCCCCCCSS[C]1[N][N][C](S1)SSCCCCCCCCCCCC', '-102.744766028688'], ['CCCCCCCCCCCCSSC(=S)/N=N\\[C]SSCCCCCCCCCCCC', '-102.721441123337'], ['CCCCCCCCCCCCSS[C]1[N]N=C(S1)SSCCCCCCCCCCCC', '-102.706423668641'], ['CCCCCCCCCCCCSSC1=N[N]C(=S)S1.[S]CCCCCCCCCCCC', '-102.747038763646'], ['CCCCCCCCCCCCSS[C]1N=N[C](S1)SSCCCCCCCCCCCC', '-102.716391644738'], ['CCCCCCCCCCCCSSc1nnc(s1)S[S].[CH2]CCCCCCCCCCC', '-102.680511792892'], ['CCCCCCCCCCCCSS[C]1[N]N=C(S1)SSCCCCCCCCCCCC', '-102.703678629573'], ['CCCCCCCCCCCC[S].[S][C]/N=N\\C(=S)SSCCCCCCCCCCCC', '-102.655618420991'], ['CCCCCCCCCCCCSS[C]1[N][N][C](S1)SSCCCCCCCCCCCC', '-102.714449469878'], ['CCCCCCCCCCCCSS[C]/N=N\\C(=S)SSCCCCCCCCCCCC', '-102.676486450894'], ['CCCCCCCCCCCCSS[C]1[N][N][C](S1)SSCCCCCCCCCCCC', '-102.702312172869'], ['CCCCCCCCCCCCSS[C][N][N]C(=S)SSCCCCCCCCCCCC', '-102.650883426888'], ['CCCCCCCCCCCCSS[C][N]N=C=S.[S]SCCCCCCCCCCCC', '-102.673888660336'], ['CCCCCCCCCCCC[S].S=C=NN=C=S.S=[S]CCCCCCCCCCCC', '-102.632130520839']]
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
range(j, len(reaction)-1)中的
k不包含
len(reaction)-1
,因此k 可以具有的值为
len(reaction)-2
,这也成为i
可以具有的最大值,因为您修改i
的唯一位置是i=k
。由于len(reaction)-2
小于len(reaction)-1
,循环条件满足,while 循环永远不会停止。k in range(j, len(reaction)-1)
doesn't includelen(reaction)-1
, So the maximun value thatk
can have islen(reaction)-2
, and that also becomes the maximun valuei
can have since the only place you modifyi
isi=k
. Sincelen(reaction)-2
is less thanlen(reaction)-1
, the loop condition is fullfilled, and the while loop never stops.我不知道你想用你的代码做什么。一些评论会有所帮助。我有一个可行的替代解决方案:
结果是:
编辑:如果您还想要名称,请更改reaction_components中的行
arr.append(reaction_array[j][1])
方法到arr.append(reaction_array[j])
。您得到的结果如下所示:I don't know what you are trying to do with your code. Some comments would help. I have an alternative solution that works:
The result is:
Edit: If you want the the names as well then change the line
arr.append(reaction_array[j][1])
inreaction_components
method toarr.append(reaction_array[j])
. The result you get looks like this: