Home > Blockchain >  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

Time:10-04

The following codes showed I tried to map a function list and got a type error of "'list' object is not callable".

The L1 is of type 'map', so I used list function to convert it and it still returned an error.

Do you have any idea about this problem? Thanks!

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)

the type of result is <class 'function'> the type of result is <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

CodePudding user response:

The below seems like a shorter way to get the same result.

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

CodePudding user response:

Please read the documentation for the map(function, iterable) function:

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

But you pass list to the function parameter.

So your example can be replaced with the next code e.g.:

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()

CodePudding user response:

 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()
  • Related