'numpy.float64' object is not iterable

匿名 (未验证) 提交于 2019-12-03 02:20:02

问题:

I'm trying to iterate an array of values generated with 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) 

This code worked fine on my office computer, but I sat down this morning to work from home on a different machine and this error came up:

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 is just an array of floats, and the script has no problem printing the contents -- just, apparently iterating through them. Any suggestions for what is causing it to break, and possible fixes?

回答1:

numpy.linspace() gives you a one-dimensional NumPy array. For example:

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

Therefore:

for index,point in my_array 

cannot work. You would need some kind of two-dimensional array with two elements in the second dimension:

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

Now you can do this:

>>> for x, y in two_d:     print(x, y)  1 2 4 5 


标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!