首先抱歉任何常见的错误。我不是母语为英语的人,而且我是蟒蛇新手。
由于lug_par
是一个包含4个项目的列表,因此第二个for
循环应该进行4次迭代。在第三个for
循环中,if的条件为真,因此它应该执行break并退出循环。我的问题是它没有进入第三次迭代,好像len(lug_par)
是2而不是4.它直接跳到cont_y = cont_y + 1
import re
string = "R(95DHS(60PST_35FDP_05MTR)_05A(95DHS"
lug_par = [s.start() for s in re.finditer('\(', string)]
lug_par_cierra = [s.start() for s in re.finditer('\)', string)]
cont_y = 0
for i in (0, len(lug_par_cierra)):
cont_x = 0
for j in (0, len(lug_par)):
if(lug_par[cont_x] > lug_par_cierra[cont_y]):
c = lug_par.index(cont_x)
borra = lug_par_cierra.index(c)
break
else:
print lug_par[cont_x]
cont_x = cont_x + 1
cont_y = cont_y + 1
答案 0 :(得分:1)
我想你忘记了range()
中for-loops
的来电。一旦纠正,它应该按照您的预期工作。
以下是更正的code
:
import re
string = "R(95DHS(60PST_35FDP_05MTR)_05A(95DHS"
lug_par = [s.start() for s in re.finditer('\(', string)]
lug_par_cierra = [s.start() for s in re.finditer('\)', string)]
cont_y = 0
for i in range(0, len(lug_par_cierra)): #range() added
cont_x = 0
for j in range(0, len(lug_par)): #range() added
if(lug_par[cont_x] > lug_par_cierra[cont_y]):
c = lug_par.index(cont_x)
borra = lug_par_cierra.index(c)
break
else:
print lug_par[cont_x]
cont_x = cont_x + 1
cont_y = cont_y + 1