内部 lambda 的 Python 循环

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23896199/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 03:37:34  来源:igfitidea点击:

Python loop for inside lambda

pythonfor-looplambda

提问by Fernando Retimo

In my code I need to simplify as much as possible my line of code. EDIT: 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:

在我的代码中,我需要尽可能地简化我的代码行。编辑:我想我不够清楚 - 它需要是一行代码。我需要在 lambda 表达式中放置一个 for 循环,如下所示:

x = lambda x: (for i in x : print i)

I just do not know how to make it happen. Thanks!

我只是不知道如何让它发生。谢谢!

采纳答案by chepner

Since a forloop is a statement (as is print, in Python 2.x), you cannot include it in a lambda expression. Instead, you need to use the writemethod on sys.stdoutalong with the joinmethod.

由于for循环是一个语句(print在 Python 2.x 中原样),您不能将它包含在 lambda 表达式中。相反,您需要将write方法sys.stdoutjoin方法一起使用。

x = lambda x: sys.stdout.write("\n".join(x) + "\n")

回答by Dair

To add on to chepner's answer for Python 3.0 you can alternatively do:

要添加 chepner 对 Python 3.0 的回答,您也可以执行以下操作:

x = lambda x: list(map(print, x))

Of course this is only if you have the means of using Python > 3 in the future... Looks a bit cleaner in my opinion, but it also has a weird return value, but you're probably discarding it anyway.

当然,这只是在你将来有办法使用 Python > 3 的情况下......在我看来,它看起来更清晰,但它也有一个奇怪的返回值,但无论如何你可能会丢弃它。

I'll just leave this here for reference.

我只是把它留在这里以供参考。

回答by Moradology

anon and chepner's answers are on the right track. Python 3.x has a print function and this is what you will need if you want to embed print within a function (and, a fortiori, lambdas).

anon 和 chepner 的回答是正确的。Python 3.x 有一个打印函数,如果你想在一个函数中嵌入打印,这就是你所需要的(更不用说,lambdas)。

However, you can get the print function very easily in python 2.x by importing from the standard library's future module. Check it out:

但是,您可以通过从标准库的未来模块中导入,在 python 2.x 中非常轻松地获得打印功能。一探究竟:

>>>from __future__ import print_function
>>>
>>>iterable = ["a","b","c"]
>>>map(print, iterable)
a
b
c
[None, None, None]
>>>

I guess that looks kind of weird, so feel free to assign the return to _ if you would like to suppress [None, None, None]'s output (you are interested in the side-effects only, I assume):

我想这看起来有点奇怪,所以如果你想抑制 [None, None, None] 的输出(你只对副作用感兴趣,我假设),请随意将 return 分配给 _:

>>>_ = map(print, iterable)
a
b
c
>>>

回答by qeatzy

If you are like me just want to print a sequence within a lambda, without get the return value (list of None).

如果你像我一样只想在 lambda 中打印一个序列,而不获取返回值(无列表)。

x = range(3)
from __future__ import print_function           # if not python 3
pra = lambda seq=x: map(print,seq) and None     # pra for 'print all'
pra()
pra('abc')

回答by Vikas

Just in case, if someone is looking for a similar problem...

以防万一,如果有人正在寻找类似的问题......

Most solutions given here are one line and are quite readable and simple. Just wanted to add one more that does not need the use of lambda(I am assuming that you are trying to use lambda just for the sake of making it a one line code). Instead, you can use a simple list comprehension.

这里给出的大多数解决方案都是一行,并且非常易读且简单。只是想再添加一个不需要使用 lambda 的(我假设您尝试使用 lambda 只是为了使其成为一行代码)。相反,您可以使用简单的列表理解。

[print(i) for i in x]

BTW, the return values will be a list on None s.

顺便说一句,返回值将是 None s上的列表。