我在 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
}
}
在 python 中,似乎不可能减去索引:
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