closur=[]
i=6
dis_tn=distance[i]
headnode=x
for k in headers[headnode]:
if distance[k]<=dis_tn:
if i in headers[k]:
closur.append(x)
closur.append(k)
closur.append(i)
else:
#break
headnode=k
continue
print "Closure of (",x,",",i,")= ",closur
在上面给出的代码中,在else部分,我需要headnode=k,在下一次迭代中,for循环需要取k的值。 例如:if,headers[headnode]=headers[0]==[5,3,1] 标题[5]==[4,1]
在第一次迭代中,取5,如果不满足if条件,则在else部分,headnode的值变为5,对于第二次迭代,需要取headers[5]。但是在这里它需要下一个标题[0],即3而不是4。你知道吗
为了解决上述问题,应该做些什么改变??你知道吗
headers[node]已经生成了一个iterable对象,它是静态的。在循环中,每次迭代都通过调用next()方法来获取值。你知道吗
for循环保持它在列表
headers[headnode]
上运行。因此,不能通过重新初始化headnode
来更改itation列表。根据上面的描述,下面的代码应该是您想要的。你知道吗相关问题 更多 >
编程相关推荐