新聞中心
之前已經(jīng)講過(guò)for循環(huán)語(yǔ)句了,今天我們就來(lái)看看怎樣在for循環(huán)中建立動(dòng)態(tài)范圍吧。

我正在遍歷列表,可以在迭代期間將元素添加到此列表中。所以問(wèn)題是循環(huán)只迭代這個(gè)列表的原始長(zhǎng)度。
代碼:
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
我已嘗試在范圍內(nèi)使用for i(1,len(srcPts)):但是,即使將更多項(xiàng)目添加到列表中,范圍也會(huì)保持不變。
問(wèn)題是當(dāng)你將它作為參數(shù)傳遞給范圍生成器時(shí),len(srcPts)只計(jì)算一次。因此,您需要有一個(gè)終止條件,在每次迭代期間重復(fù)計(jì)算srcPts的當(dāng)前長(zhǎng)度。有很多方法可以做到這一點(diǎn),例如:
while i < len(srcPts): ....
以上就是python中for循環(huán)建立動(dòng)態(tài)范圍的方法。更多Python學(xué)習(xí)推薦:PyThon學(xué)習(xí)網(wǎng)教學(xué)中心。
網(wǎng)頁(yè)題目:創(chuàng)新互聯(lián)Python教程:Python中for循環(huán)的動(dòng)態(tài)范圍
文章出自:http://m.fisionsoft.com.cn/article/djepsgi.html


咨詢
建站咨詢
