Python error: 'list' object is not callable after converting the map function to list

Python error: 'list' object is not callable after converting the map function to list

以下代码显示我尝试映射一个函数列表并得到类型错误“'list'对象不可调用”。

L1的类型是'map',所以我用list函数转换还是报错

你知道这个问题吗?谢谢!

import math
func_list=[math.sin, math.cos, math.exp]
result=lambda L: map(func_list, L)
L=[0,0,0]
L1=result(L)
for x in L1:
    print(x)

结果类型是<class 'function'>结果类型是<class 'map'>

Traceback (most recent call last) 
<ipython-input-22-17579bed9240> in <module>
          6 print("the type of result is " + str(type(result)))
          7 print("the type of result is " + str(type(L1)))
    ----> 8 for x in L1:
          9     print(x)
    
    TypeError: 'list' object is not callable

请阅读 map(function, iterable) 函数的文档:

https://docs.python.org/3/library/functions.html#map

但是您将列表传递给 function 参数。

因此您的示例可以替换为下一个代码,例如:

import math

func_list = [math.sin, math.cos, math.exp]

result=lambda L: map(lambda x: map(lambda func: func(x), func_list), L)

L = [0, 0, 0]
L1 = result(L)

for x in L1:
    for value in x:
        print(value, end=' ')
    
    print()

以下似乎是获得相同结果的更短方法。

import math
func_list=[math.sin, math.cos, math.exp]
lst = [f(0) for f in func_list]
print(lst)
 import math

 func_list = [math.sin, math.cos, math.exp]

 result=lambda L: map(lambda x: map(lambda func: func(x), func_list), L)

 L = [0, 0, 0]
 L1 = result(L)

 for x in L1:
 for value in x:
    print(value, end=' ')

 print()