我遇到了在python中复制for循环的问题。Python for循环问题
这是我的c风格的脚本,只有for循环方面。
for ($c=0 ; $c<size($verts); $c++)
{
//// do some code here
$verts = remove($verts[c],$verts); /// remove this item from the $verts list
$c-=1; /// lower the index becuase an item was removed
for ($n=0 ; $n<size($verts); $n++)
{
if($condition)
$verts = remove($verts[$n],$verts); /// remove this item from the $verts list
$n-=1; /// lower the index becuase an item was removed
}
}
在蟒蛇有它似乎并不才有可能减去指数:
item = range(10);
for i in item :
del item[i]
i-=1 # this doesn't do anything for the next interation
什么是用Python语言编写上面的C循环的最佳方式?
编辑: 这里是循环的工作,因为我需要在Python
count = range(len(vtx))
for num in count:
if len(vtx) != 0:
p.append ([]); p[len(p)-1].append(vtx[0])
v.append ([]); v[len(p)-1].append(vec[0])
a = vec[0]
del vtx[0]
del vec[0]
count2 = range(len(vtx))
n2 = 0;
for num2 in count2:
b = vec[n2]
distance = math.sqrt((a[0] - b[0])**2 + (a[1]- b[1])**2 + (a[2]- b[2])**2);
if distance <= threshold :
p[len(p)-1].append (vtx[n2])
v[len(p)-1].append (vec[n2])
vtx.remove(vtx[n2])
vec.remove(vec[n2])
else:
n2+=1
感谢所有的答案。我最终做了一些不太pythonic的事情,因为我仍然对索引有很多麻烦,并且意识到我可以简化我的c代码。 (请参阅带有工作代码的更新问题)但是所有的答案都很有帮助,现在我明白了python的工作原理。 – fghajhe 2013-03-16 19:40:46
@fghajhe我发布了一个基于你的编辑的版本,这个版本更容易遵循。不确定它是否相关,但你的算法是依赖于顺序的。如果你重新排列你的vtx/vec对,你会得到不同的结果。 – 2013-03-17 00:03:49