python - “列表”对象在列表理解中不可调用

标签 python list-comprehension

我正在构建这段代码并构建了输出我想要的绘图的第一部分,然后我开始处理绘图部分的第二半部分,也许在运行 10 左右后我的代码的前半部分停止了在职的。我本来不想做任何事情,但现在我无法取回它,并且我的 for 循环收到错误 'list' object is not callable' 。尽管我使用的是数组,但它说这个错误。我尝试了不同的列表理解语法以及将数组设置为集合、列表和字符串。不太确定该怎么做,所以任何帮助或尝试的事情都会有帮助。

import numpy as np
import pylab as plt

#Before the explosion
t1 = np.asarray(range(0, 5))
t2 = np.linspace(0, 4 , 1)
g = 1.0
vx = 4.0
vy = 4.0

def x1 (t):
    return (vx*t)

def y1 (t):
    return(vy*t -(0.5*g*(t**2.0)))

x1 = [x1(t) for t in t1]
y1 = [y1(t) for t in t1]

x2 = [x1(t) for t in t2]
y2 = [y1(t) for t in t2]

#after the explosion

'''
t3 = range(5,10)
t4 = np.linspace(5, 9 , 1000)

vx = 5
vy = 3

def x2 (t):
    return (16+vx)

def y2 (t):
    return(vy*t -(0.5*g*(t**2)))

'''
plt.scatter(x1,y1, color='blue',marker='+',s=100.0,  label = '')
plt.plot(x2,y2, color='red',marker=None,  label = '')

plt.show()

输出:

     20 y1 = [y1(t) for t in t1]
     21 
---> 22 x2 = [x1(t) for t in t2]
     23 y2 = [y1(t) for t in t2]
     24 

TypeError: 'list' object is not callable 

最佳答案

您似乎想调用定义的函数来获取 x2 的值。尝试更改定义中函数的名称(或更改变量 x1 和 y1 的名称)。

def xfunc(t):
  return (vx*t)
def yfunc(t):
  return(vy*t -(0.5*g*(t**2.0)))

x1 = [xfunc(t) for t in t1]
y1 = [yfunc(t) for t in t1]

x2 = [xfunc(t) for t in t2]
y2 = [yfunc(t) for t in t2]

关于python - “列表”对象在列表理解中不可调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46231622/

相关文章:

python - 在 __init__ 函数中循环?

具有多个循环的 Python 列表理解

python - Tensorflow 磁贴抛出 TypeError : List of Tensors when single Tensor expected

python - 如何限制循环的迭代?

python - 更改字符串中某些字符大小写的函数

python - 列表理解导致 "name ... is not defined"错误

Python广播ValueError乘以numpy矩阵

python - 以键和值作为子列表的反转字典

python - 如何使用 Robust PCA 输出作为传统 PCA 的主成分(特征)向量

python - 创建比例数据框