我正在编写一个将新元素添加到现有列表的循环。但是我发现,当我向列表中添加新元素时,列表的长度增加了一个,但是列表中的所有元素都被新元素替换。我期待着你的手!
我试图编写一个简单的代码以将元素添加到控制台循环中的现有列表中,但是结果是正确的。所以我真的很想知道以前的程序是我的错。
#construct the boundary of the set and the given point
beginPoint = middlePoint(projectionInSet(givenPoint), givenPoint)
pointSetBoundary = [beginPoint,]
print(pointSetBoundary)
#rightside of the beginning point
rightPoint = beginPoint
print("Begin Point: ", rightPoint)
projection = projectionInSet(rightPoint)
print("Begin Projection: ", projection )
[k, b] = perpendicualrBisector(projectionInSet(givenPoint), givenPoint)
stepRight = 0.01
while rightPoint[0] <= 10 and rightPoint[1] <= 10 and rightPoint[0] >= 0 and rightPoint[1] >= 0:
rightPoint[0] = rightPoint[0] + stepRight
rightPoint[1] = k * rightPoint[0] + b
if distance(projectionInSet(rightPoint), projection) > 0.000001:
projection = projectionInSet(rightPoint)
[k, b] = perpendicualrBisector(projection, givenPoint)
if k >= 0:
stepRight = 0.01
else:
stepRight = -0.01
pointSetBoundary.append(rightPoint)
print(pointSetBoundary)
print("Right Boundaries completed!")
我希望将新的rightPoint添加到以前的列表中而不更改现有元素。
答案 0 :(得分:0)
发布问题后,我突然意识到,我添加到列表中的元素一直都在变化。非常抱歉为了节省您的时间,我关闭了这个问题。
在while循环的开始,我将rightPoint点的值更改为。但是,列表的前一个元素共享相同的存储空间。因此,它们的内容会同时更改。