【问题标题】:Python Error 'list' object has no attribute 'is_Number'Python 错误“列表”对象没有属性“is_Number”
【发布时间】:2021-12-04 14:20:11
【问题描述】:

我的 python 代码有一些问题,我正在使用 sympy 并想创建一个 Lambda 函数,该函数将实数列表 [a1,a2,...,an] 作为输入并返回一个列表实数 [b1,b2,...,bn] 其中 bi=cos(ai) +sin(ai)。 iε[1, 2, ..., n] 但是,我遇到了一个属性错误,即

AttributeError: 'list' object has no attribute 'is_Number'

这是我的代码(我很新,知道这不是最好的):

import sympy

# defining symbol 'x'
x = sympy.symbols('x')

# defining a lambda function that takes a list of values and returns the
# result of applying the formula cos(x)+sin(x) for each input value provided
f = sympy.Lambda(x, sympy.cos(x) + sympy.sin(x))

# testing by creating a list
input_list = [-1, -0.5, 0, 0.5, 1]
# calling f method to fetch the results of applying cos()+sin() for each value in input_list
output_list = f(input_list)

# printing both lists
print(input_list)
print(output_list)

【问题讨论】:

    标签: python list sympy attributeerror


    【解决方案1】:

    在您的情况下,f 不采用列表,而只是一个值 x,因此为了获得您的输出,您需要这样做...

    output_list = [sympy.N(f(x)) for x in input_list]
    print(output_list)
    

    这将输出以下内容供您输入

    [-0.301168678939757, 0.398157023286170, 1.00000000000000, 1.35700810049458, 1.38177329067604]
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-05-25
      • 2022-01-20
      • 1970-01-01
      • 2014-05-14
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多