首页 > 解决方案 > 更改两行的顺序时,我得到 TypeError: 'numpy.ndarray' object is not callable

问题描述

这有效:

import numpy as np 
import matplotlib.pyplot as plt 

def n(vg):
    Eta = 1.825e-5
    B = 7e-3
    Rho= 900
    g = 9.81
    # r = (9 * Eta * vg/(2* Rho* g))**0.5
    V = 30
    P = 1
    n = (9 * np.pi * 2 **0.5 * Eta**(3/2) /(Rho * g)**(0.5)) *(1 +B/(((9* Eta* vg/(2 * Rho * g))**0.5)*P))**(-3/2) * ((vg**3/2)/V)
    return n

def list_of_n(vg_start,vg_end,d_vg):
    n_vg=int((vg_end-vg_start)/d_vg)
    if n_vg<1:
        print("Error: d_vg is in a way that number of data points is less than one! ")
        raise SystemExit
    vg= np.linspace(vg_start,vg_end,n_vg)
    l=list((i,n(i)) for i in vg)
    return np.array(l)


print(list_of_n(0.5,2.5,0.1))
vg,n=list_of_n(0.5,2.5,0.1).T
plt.plot(vg,n,'o',c='Blue')
plt.xlabel(r'$n$',fontsize=14)
plt.ylabel(r'${v}_{g}$',fontsize=14)
plt.show()

但是当我将打印行移到末尾时:

#Same as above
vg,n=list_of_n(0.5,2.5,0.1).T
plt.plot(vg,n,'o',c='Blue')
plt.xlabel(r'$n$',fontsize=14)
plt.ylabel(r'${v}_{g}$',fontsize=14)
plt.show()
print(list_of_n(0.5,2.5,0.1))

我收到以下错误:

Traceback (most recent call last):
  File "3.py", line 30, in <module>
    print(list_of_n(0.5,2.5,0.1))
  File "3.py", line 21, in list_of_n
    l=np.array(list((i,n(i)) for i in vg))
  File "3.py", line 21, in <genexpr>
    l=np.array(list((i,n(i)) for i in vg))
TypeError: 'numpy.ndarray' object is not callable

我在“numpy.ndarray”中意识到 object is not callable 错误为什么 numpy.ndarray is object is not callable in my simple for python loop that this error意味着我试图将numpy数组作为函数调用,但这与我的问题

标签: pythonnumpy

解决方案


推荐阅读