22

我正在尝试迭代使用 numpy.linspace 生成的值数组:

slX = numpy.linspace(obsvX, flightX, numSPts)
slY = np.linspace(obsvY, flightY, numSPts)

for index,point in slX:
    yPoint = slY[index]
    arcpy.AddMessage(yPoint)

这段代码在我的办公室电脑上运行良好,但我今天早上坐下来在另一台机器上在家工作,出现了这个错误:

File "C:\temp\gssm_arcpy.1.0.3.py", line 147, in AnalyzeSightLine
  for index,point in slX:
TypeError: 'numpy.float64' object is not iterable

slX只是一个浮点数组,脚本打印内容没有问题 - 只是,显然迭代它们。关于导致它崩溃的任何建议以及可能的修复方法?

4

1 回答 1

9

numpy.linspace()给你一个一维 NumPy 数组。例如:

>>> my_array = numpy.linspace(1, 10, 10)
>>> my_array
array([  1.,   2.,   3.,   4.,   5.,   6.,   7.,   8.,   9.,  10.])

所以:

for index,point in my_array

不能工作。您将需要某种二维数组,在第二维中有两个元素:

>>> two_d = numpy.array([[1, 2], [4, 5]])
>>> two_d
array([[1, 2], [4, 5]])

现在你可以这样做:

>>> for x, y in two_d:
    print(x, y)

1 2
4 5
于 2013-05-31T20:53:35.913 回答