我在遍歷一個列表。迭代期間可以將一個元素添加到此列表中。所以問題是循環只能遍歷這個列表的原始長度。你如何創建一個動態範圍的循環?
我的代碼:
i = 1
for p in srcPts[1:]: # skip the first item.
pt1 = srcPts[i - 1]["Point"]
pt2 = p["Point"]
d = MathUtils.distance(pt1, pt2)
if (D + d) >= I:
qx = pt1.X + ((I - D)/d) * (pt2.X - pt1.X)
qy = pt1.Y + ((I - D)/d) * (pt2.Y - pt1.Y)
q = Point(float(qx), float(qy))
# Append new point q.
dstPts.append(q)
# Insert 'q' at position i in points s.t. 'q' will be the next i.
srcPts.insert(i, {"Point": q})
D = 0.0
else:
D += d
i += 1
我一直在使用for i in range(1, len(srcPts)):
嘗試,但再次的範圍內保持不變,即使經過項目已被添加到列表中。
謝謝,我這樣一個Python菜鳥。 –