在 while 语句 python 中分配变量

assign variable in while statement python

嗨,我正在尝试从 matplotlib.pyplot.ginput 中获取一组积分。 我的想法是在 actuaPoint!=lastPoint

时将点保存在列表中

有没有办法做到这一点:

lastPoint = None
pointList = []

actualPoint = plt.ginput(1)
while (actualPoint=plt.input()) != lastPoint:
    pointList.append(actualPoint)
    lastPoint= actualPoint

?恢复我想知道是否有办法在 while 语句中进行变量赋值

您需要使用赋值 表达式,使用 := 运算符。

point, = pointList = [plt.ginput(1)]
while (next_point := plt.input()) != point:
    pointList.append(next_point)
    point = next_point