[英]print using Lambda function in python 3
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.我想使用 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.我如何使用 lambda function 打印以显示这样的 output。
thanks谢谢
Zep捷普
Lambda function is a function, so you need to use parentheses after the function name to actually call it, just like any other function: Lambda函数是一个函数,因此您需要像其他任何函数一样在函数名称后使用括号来实际调用它:
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
. 但是出于打印输出的目的,最好使用正则函数而不是lambda函数,这是为了快速表达式而不是起作用并返回
None
函数。
For debugging purposes it can be interesting to print from a lambda function.出于调试目的,从 lambda function 打印可能很有趣。
You can use a list, tuple or dict to do just that:您可以使用列表、元组或字典来做到这一点:
Suppose you have lambda function containing a simple test:假设您有 lambda function 包含一个简单的测试:
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)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.