0

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
2
  • map expects a function as its first argument, but [math.sin, math.cos, math.exp] is not a function, it's a list. What are you expecting it to do? Commented Oct 3, 2021 at 13:15
  • This question is to calculate the math(sin(0)), math(sin(0)), math(sin(0)) with a function list and lambda function. Thank you for clarifying this issue. Commented Oct 3, 2021 at 13:30

3 Answers 3

1

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

Sign up to request clarification or add additional context in comments.

3 Comments

@AmyZeng L list is a set of different values or you just want to calculate different functions for one value?
it is to calculate [math.sin(0), math.cos(0), math.exp(0)] with a function list and with map function. The values assigned to sin/cos/exp should be changable.
@AmyZeng, okay, then this solution makes sense
1

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)

1 Comment

Thank you, it is neat, while this is an coding exercise that requires "map" and "lambda" expressions to be in the codes.
1
 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()

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.