Reputation: 1352
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
Upvotes: 1
Views: 1112
Reputation: 275
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)
Upvotes: 0
Reputation: 106946
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
.
Upvotes: 3