所以我在python上遇到了一个奇怪的问题。我正在使用下面的代码来创建对象所在的位置。这是我的代码:
def GoForward(self, duration):
if (self.TowardsX - self.X == 0):
Myro.robot.motors(self.Speed, self.Speed)
Myro.wait(abs(duration))
Myro.robot.stop()
#get the amount of points forward
divisible = (int) (duration / self.Scale)
#add them to the direction
self.TowardsY += divisible
tempY = self.Y
for y in xrange(self.Y, divisible + tempY):
if (y % self.Scale == 0):
self.Plot[(int) (self.X)][y] = 1
return
#calc slope
slope = (self.TowardsY - self.Y) / (self.TowardsX - self.X)
tempX = self.X
tempY = self.Y
#go forward
#get the amount of points forward
divisible = duration / self.Scale
#add them to the direction
self.TowardsX += divisible
self.TowardsY += divisible
Xs = []
Ys = []
for x in xrange(self.X, tempX + divisible):
#find out if it is a plottable point
if (((slope * (x - self.X)) + self.Y) % self.Scale == 0.0):
Xs.append(x)
Ys.append((int)((slope * (x - self.X)) + self.Y))
#Plot the points
for i in xrange(0, len(Xs)):
for j in xrange(0, len(Ys)):
if (self.Plot[Xs[i]][Ys[j]] == 0):
self.Plot[Xs[i]][Ys[j]] = 1
self.X += divisible
self.Y += divisible但是,当我调用GoForward(2)时,它用一列填充了五列,而不是少数几个点。示例:
[[0,0,0,0,1,1,0,0,0,0]
[0,0,0,0,1,1,0,0,0,0]
[0,0,0,0,1,1,0,0,0,0]
[0,0,0,0,1,1,0,0,0,0]
[0,0,0,0,1,1,0,0,0,0]
[0,0,0,0,1,1,0,0,0,0]
[0,0,0,0,1,1,0,0,0,0]
[0,0,0,0,1,1,0,0,0,0]
[0,0,0,0,1,1,0,0,0,0]
[0,0,0,0,1,1,0,0,0,0]]基于提供给GoForward(n)的参数,它创建了许多包含0的列。为什么会发生这种行为?我的代码不应该产生这种效果,但是我对python缺乏经验,所以这就是为什么会发生这种情况吗?提前感谢
编辑
因此,我已经将绘制点的代码更改为
for i in xrange(0, len(Xs)):
if (self.Plot[Xs[i]][Ys[i]] == 0):
self.Plot[Xs[i]][Ys[i]] = 1它将具有正确的值,但是它仍然会产生这种奇怪的行为,问题在于这里的代码。
编辑2
当我使用代码时:
self.Plot[3][3] = 1它仍然生成一个数组:
[[0, 0, 0, 1, 0, 0, 0, 0, 0, 0]
[0, 0, 0, 1, 0, 0, 0, 0, 0, 0]
[0, 0, 0, 1, 0, 0, 0, 0, 0, 0]
[0, 0, 0, 1, 0, 0, 0, 0, 0, 0]
[0, 0, 0, 1, 0, 0, 0, 0, 0, 0]
[0, 0, 0, 1, 0, 0, 0, 0, 0, 0]
[0, 0, 0, 1, 0, 0, 0, 0, 0, 0]
[0, 0, 0, 1, 0, 0, 0, 0, 0, 0]
[0, 0, 0, 1, 0, 0, 0, 0, 0, 0]
[0, 0, 0, 1, 0, 0, 0, 0, 0, 0]]发布于 2014-03-10 02:41:57
那么,要生成这个网格,您要显示的是打印self.Plot吗?你说这个网格被初始化为0?self.Plot到底是什么?一张名单就这样?当您在运行该循环之前打印self.Plot时,它是否打印您期望的内容(我认为应该是零)?
因此,如果Xs和Ys是应该是1的点,则可以简化代码,使用一个可绘制点的列表:
plottable_points = []
# for loop
plottable_points.append( (x, int((slope * (x - self.X)) + self.Y)) )
for x, y in plottable_points:
self.Plot[x][y] = 1我不确定self.Plot是如何初始化或使用的,但是如果您在每一步之前和之后打印东西,您应该能够确定您的逻辑哪里出错了。
编辑1:额外的小蟒蛇提示:
for x, y in zip(Xs, Ys):
self.Plot[x][y] = 1执行与我的第一个代码示例相同的操作,但使用变量。
编辑2:
问题实际上在于如何初始化self.Plot。当您重复这样的列表时,外部列表就变成了指向同一个对象的pointers...all列表(在本例中是一个列表)。因此,当您说self.Plot[3][3] = 1时,实际上是在每一行中设置该列。尝试使用类似的方法初始化self.Plot (可能有更好的方法,但我很累):
self.Plot = []
for col in range(height * multiplyBy):
self.Plot.append([0] * width * multiplyBy)
# or:
self.Plot = [ [0] * width * multiply for col in range(height * multiplyBy) ]发布于 2014-03-10 01:33:37
我想说的是,您应该先做一个简单的导入(来自未来导入部门),然后再做任何修改。在我看来,问题在于分歧。在Python2.7中,它返回一个整数。看一看这个:
>>> 4/3
1
>>> 4//3
1但是如果你导入新的部门功能..。
>>> from __future__ import division
>>> 4/3
1.3333333333333333https://stackoverflow.com/questions/22290577
复制相似问题