内部lambda的Python循环 [英] Python loop for inside lambda
本文介绍了内部lambda的Python循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在我的代码中,我需要尽可能简化我的代码行. 我想我还不够清楚-它需要是一行代码. 我需要在lambda表达式中放入for循环,诸如此类:
In my code I need to simplify as much as possible my line of code. I think I'm not clear enough - it needs to be one line of code. I need to put a for loop inside a lambda expression, something like that:
x = lambda x: (for i in x : print i)
我只是不知道如何实现它. 谢谢!
I just do not know how to make it happen. Thanks!
推荐答案
由于for
循环是一条语句(Python 2.x中的print
也是这样),因此您不能将其包括在lambda表达式中.相反,您需要在sys.stdout
上同时使用write
方法和join
方法.
Since a for
loop is a statement (as is print
, in Python 2.x), you cannot include it in a lambda expression. Instead, you need to use the write
method on sys.stdout
along with the join
method.
x = lambda x: sys.stdout.write("\n".join(x) + "\n")
这篇关于内部lambda的Python循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文