numpy.float64 不可迭代

numpy.float64 is not iterable

我正在尝试打印一个使用 numpy 数组和列表中的多个参数的函数,但我一直收到错误 "numpy.float 64 object is not iterable"。我在论坛上查看了几个关于这个主题的问题并尝试了不同的答案,但 none 似乎有效(或者我可能做错了什么我在 python 仍然是初学者)但这一切归结为同一件事,我被困住了,希望你们能提供帮助。我正在使用 python 2.7,这是代码:

编辑:包含错误消息并将打印更改为 "print(T, (obj(T),))"

   from __future__ import division
   import numpy as np
   import random

   K = np.array([1, 1, 1, 1, 1, 1, 1, 1, 1,])
   x = len(K)
   #Production rates and demand rates of products setup costs and holding costs (p, d, c, h)
   p = np.array([193, 247, 231, 189, 159])
   d = np.array([16, 16, 21, 19, 23])
   #c1 = np.array([random.random() for _ in range(x)]) use these values as test values for c
   c = [0.752, 0.768, 0.263, 0.152, 0.994, 0.449, 0.431, 0.154, 0.772]
   h = [0.10*c[i]/240 for i in range(x)]
   n = len(p)
   t = [10.76, 74.61, 47.54, 29.40, 45.00, 90.48, 17.09, 85.19, 35.33]


   def obj(T):
      for i in range(n):
         for q in range(x):
             for k in range(x):
                 return ((1. / T) * c[q] + sum((.5*h[k]*(p[i]-d[i])* (p[i]/d[i])*(t[k])**2)))

   for T in range(200, 900):
       print(T, (obj(T),))

 runfile('C:/Users/Jasper/Anaconda2/Shirodkar.py',     wdir='C:/Users/Jasper/Anaconda2')
 Traceback (most recent call last):

File "<ipython-input-1-0cfdc6b9fe69>", line 1, in <module>
runfile('C:/Users/Jasper/Anaconda2/Shirodkar.py',  wdir='C:/Users/Jasper/Anaconda2')

  File "C:\Users\Jasper\Anaconda2\lib\site-packages\spyderlib\widgets\externalshell\sitecustomize.py", line 714, in runfile
execfile(filename, namespace)

  File "C:\Users\Jasper\Anaconda2\lib\site-  packages\spyderlib\widgets\externalshell\sitecustomize.py", line 74, in execfile
exec(compile(scripttext, filename, 'exec'), glob, loc)

  File "C:/Users/Jasper/Anaconda2/Shirodkar.py", line 24, in <module>
print(T, (obj(T),))

   File "C:/Users/Jasper/Anaconda2/Shirodkar.py", line 21, in obj
return ((1. / T) * c[q] + sum((.5*h[k]*(p[i]-d[i])*(p[i]/d[i])*(t[k])**2)))

 TypeError: 'numpy.float64' object is not iterable

我怀疑问题出在这里:

sum((.5*h[k]*(p[i]-d[i])* (p[i]/d[i])*(t[k])**2))

那个表达式的最终结果是一个浮点数,不是吗? sum() 有什么用?