我的 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)
uj5u.com熱心網友回復:
在你的情況下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]
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/322310.html
