1

I have the following lists:

para = ['bodyPart', 'shotQuality', 'defPressure', 'numDefPlayers', 'numAttPlayers', 'shotdist', 'angle', 'chanceRating', 'type']

value = [ 0.09786083,  2.30523761, -0.05875112,  
0.07905136, -0.1663424 ,-0.73930942, -0.10385882,  0.98845481,  0.13175622]

I want to print using lambda function.

what i want to show is as follow:

coefficient for 
bodyPart is 0.09786083
shotQuality is 2.30523761
defPressure is -0.05875112
numDefPlayers is 0.07905136 and so on

I use the following code:

b = lambda x:print(para[x],'is',coeff[x])
print('Coefficient for')
print(b)

and it does not work and only shows this:

Coefficient for
<function <lambda> at 0x000001A8A62A0378>

how can i use lambda function to print to show such output.

thanks

Zep

2 Answers 2

3

Lambda function is a function, so you need to use parentheses after the function name to actually call it, just like any other function:

for i in range(len(para)):
    print(b(i))

But for the purpose of printing output it's better to use a regular function instead of a lambda function, which is meant for quick expressions rather than functions that do work and return None.

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

Comments

0

For debugging purposes it can be interesting to print from a lambda function.

You can use a list, tuple or dict to do just that:

Suppose you have lambda function containing a simple test:

lambda x:  x > 10

You can then rewrite to print the incoming variable:

lambda x: [print(x), x > 10][-1]

We can now test:

mylambda = lambda x: [print(x), x > 10][-1]
mylambda(12), mylambda(8)

Which yields:

12
8

(True, False)

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.