Python 'numpy.float64' 对象不可迭代
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16862459/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
'numpy.float64' object is not iterable
提问by Erica
I'm trying to iterate an array of values generated with numpy.linspace:
我正在尝试迭代使用 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
slXis 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?
slX只是一个浮点数组,脚本打印内容没有问题——只是,显然是遍历它们。有关导致其损坏的原因以及可能的修复方法的任何建议?
采纳答案by Mike Müller
numpy.linspace()gives you a one-dimensional NumPy array. For example:
numpy.linspace()给你一个一维的 NumPy 数组。例如:
>>> 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

