numpy.float64不可迭代

时间:2016-10-12 14:31:31

标签: python function numpy iterable

我试图打印一个使用numpy数组和列表中的几个参数的函数,但我一直得到错误" numpy.float 64对象不可迭代"。我已经在论坛上看了几个关于这个主题的问题并尝试了不同的答案,但似乎都没有用(或者我可能做错了我还是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

1 个答案:

答案 0 :(得分:2)

我怀疑问题在这里:

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

该表达式的最终结果是浮点数,不是吗?什么是sum()?