Python:打印生成器表达式? [英] Python: print a generator expression?

查看:63
本文介绍了Python:打印生成器表达式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Python shell中,如果我输入列表理解,例如:

In the Python shell, if I enter a list comprehension such as:

>>> [x for x in string.letters if x in [y for y in "BigMan on campus"]]

我得到一个很好的打印结果:

I get a nicely printed result:

['a', 'c', 'g', 'i', 'm', 'n', 'o', 'p', 's', 'u', 'B', 'M']

与字典理解相同:

>>> {x:x*2 for x in range(1,10)}
{1: 2, 2: 4, 3: 6, 4: 8, 5: 10, 6: 12, 7: 14, 8: 16, 9: 18}

如果输入生成器表达式,则不会得到这样的友好响应:

If I enter a generator expression, I get not such a friendly response:

>>> (x for x in string.letters if x in (y for y in "BigMan on campus"))
<generator object <genexpr> at 0x1004a0be0>

我知道我可以做到:

>>> for i in _: print i,
a c g i m n o p s u B M

除此以外(或编写一个辅助函数),我是否可以轻松地在交互式shell中评估并打印该生成器对象?

Other than that (or writing a helper function) can I easily evaluate and print that generator object in the interactive shell?

推荐答案

快速解答:

在生成器表达式周围执行 list()(几乎)完全等同于在其周围加上 [] 括号.是的,你可以做

Doing list() around a generator expression is (almost) exactly equivalent to having [] brackets around it. So yeah, you can do

>>> list((x for x in string.letters if x in (y for y in "BigMan on campus")))

但是你也可以做

>>> [x for x in string.letters if x in (y for y in "BigMan on campus")]

是的,这会将生成器表达式转换为列表推导.这是同一件事,并在其上调用list().因此将生成器表达式放入列表的方法是在方括号中加上括号.

Yes, that will turn the generator expression into a list comprehension. It's the same thing and calling list() on it. So the way to make a generator expression into a list is to put brackets around it.

详细说明:

生成器表达式是裸"的 for 表达式.像这样:

A generator expression is a "naked" for expression. Like so:

x*x for x in range(10)

现在,您不能将其本身放在一行上,您会收到语法错误.但是您可以在其周围加上括号.

Now, you can't stick that on a line by itself, you'll get a syntax error. But you can put parenthesis around it.

>>> (x*x for x in range(10))
<generator object <genexpr> at 0xb7485464>

这有时被称为生成器理解,尽管我认为正式名称仍然是生成器表达式,但实际上并没有什么区别,括号只是为了使语法有效.如果要将其作为唯一参数传递给函数,则不需要它们,例如:

This is sometimes called a generator comprehension, although I think the official name still is generator expression, there isn't really any difference, the parenthesis are only there to make the syntax valid. You do not need them if you are passing it in as the only parameter to a function for example:

>>> sorted(x*x for x in range(10))
[0, 1, 4, 9, 16, 25, 36, 49, 64, 81]

基本上,Python 3和Python 2.7中可用的所有其他理解只是生成器表达式周围的语法糖.设置理解力:

Basically all the other comprehensions available in Python 3 and Python 2.7 is just syntactic sugar around a generator expression. Set comprehensions:

>>> {x*x for x in range(10)}
{0, 1, 4, 81, 64, 9, 16, 49, 25, 36}

>>> set(x*x for x in range(10))
{0, 1, 4, 81, 64, 9, 16, 49, 25, 36}

词典理解:

>>> dict((x, x*x) for x in range(10))
{0: 0, 1: 1, 2: 4, 3: 9, 4: 16, 5: 25, 6: 36, 7: 49, 8: 64, 9: 81}

>>> {x: x*x for x in range(10)}
{0: 0, 1: 1, 2: 4, 3: 9, 4: 16, 5: 25, 6: 36, 7: 49, 8: 64, 9: 81}

并列出Python 3下的理解力:

And list comprehensions under Python 3:

>>> list(x*x for x in range(10))
[0, 1, 4, 9, 16, 25, 36, 49, 64, 81]

>>> [x*x for x in range(10)]
[0, 1, 4, 9, 16, 25, 36, 49, 64, 81]

在Python 2下,列表理解不仅是语法糖.但是唯一的区别是x在Python 2下会泄漏到名称空间中.

Under Python 2, list comprehensions is not just syntactic sugar. But the only difference is that x will under Python 2 leak into the namespace.

>>> x
9

在Python 3下,您会得到

While under Python 3 you'll get

>>> x
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'x' is not defined

这意味着在Python中很好地打印生成器表达式的内容的最佳方法是对其进行列表理解!但是,如果您已经具有生成器对象,那么这显然将不起作用.这样做只会列出一个生成器的列表:

This means that the best way to get a nice printout of the content of your generator expression in Python is to make a list comprehension out of it! However, this will obviously not work if you already have a generator object. Doing that will just make a list of one generator:

>>> foo = (x*x for x in range(10))
>>> [foo]
[<generator object <genexpr> at 0xb7559504>]

在这种情况下,您将需要调用 list():

In that case you will need to call list():

>>> list(foo)
[0, 1, 4, 9, 16, 25, 36, 49, 64, 81]

虽然可以,但是有点愚蠢:

Although this works, but is kinda stupid:

>>> [x for x in foo]
[0, 1, 4, 9, 16, 25, 36, 49, 64, 81]

这篇关于Python:打印生成器表达式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆