• <small id='vWELe'></small><noframes id='vWELe'>

      <bdo id='vWELe'></bdo><ul id='vWELe'></ul>
  • <i id='vWELe'><tr id='vWELe'><dt id='vWELe'><q id='vWELe'><span id='vWELe'><b id='vWELe'><form id='vWELe'><ins id='vWELe'></ins><ul id='vWELe'></ul><sub id='vWELe'></sub></form><legend id='vWELe'></legend><bdo id='vWELe'><pre id='vWELe'><center id='vWELe'></center></pre></bdo></b><th id='vWELe'></th></span></q></dt></tr></i><div id='vWELe'><tfoot id='vWELe'></tfoot><dl id='vWELe'><fieldset id='vWELe'></fieldset></dl></div>

      1. <tfoot id='vWELe'></tfoot><legend id='vWELe'><style id='vWELe'><dir id='vWELe'><q id='vWELe'></q></dir></style></legend>

      2. 如何打印生成器表达式?

        How to print a generator expression?(如何打印生成器表达式?)
          <tbody id='824Bb'></tbody>

      3. <small id='824Bb'></small><noframes id='824Bb'>

            <bdo id='824Bb'></bdo><ul id='824Bb'></ul>
            <legend id='824Bb'><style id='824Bb'><dir id='824Bb'><q id='824Bb'></q></dir></style></legend>
            1. <tfoot id='824Bb'></tfoot>

                  <i id='824Bb'><tr id='824Bb'><dt id='824Bb'><q id='824Bb'><span id='824Bb'><b id='824Bb'><form id='824Bb'><ins id='824Bb'></ins><ul id='824Bb'></ul><sub id='824Bb'></sub></form><legend id='824Bb'></legend><bdo id='824Bb'><pre id='824Bb'><center id='824Bb'></center></pre></bdo></b><th id='824Bb'></th></span></q></dt></tr></i><div id='824Bb'><tfoot id='824Bb'></tfoot><dl id='824Bb'><fieldset id='824Bb'></fieldset></dl></div>
                1. 本文介绍了如何打印生成器表达式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

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

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

                  我得到了打印得很好的结果:

                  ['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}
                  

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

                  >>> (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中轻松计算和打印生成器对象?

                  推荐答案

                  快速回答:

                  对生成器表达式执行list()操作(几乎)完全等同于将[]括起来。所以,是的,您可以

                  >>> 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")]
                  

                  是的,这将把生成器表达式转换为列表理解。它和它上面的调用列表()是一样的。因此,将生成器表达式转换为列表的方法是将其括起来。

                  详细说明:

                  生成器表达式是"裸"for表达式。如下所示:

                  x*x for x in range(10)
                  

                  现在,您不能将其单独放在一行中,这样会出现语法错误。但是你可以用括号把它括起来。

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

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

                  >>> sorted(x*x for x in range(10))
                  [0, 1, 4, 9, 16, 25, 36, 49, 64, 81]
                  
                  基本上,Python3和Python2.7中提供的所有其他理解都只是围绕生成器表达式的语法糖。设置理解:

                  >>> {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下的列表理解:

                  >>> 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]
                  

                  在Python2中,列表理解不仅仅是语法上的糖。但唯一的区别是,Python2下的x将泄漏到命名空间。

                  >>> x
                  9
                  

                  在Python 3下,您将获得

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

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

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

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

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

                  虽然这行得通,但有点愚蠢:

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

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

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  GUI Freezes while downloading PyQt5 and Pytube(GUI在下载PyQt5和Pytube时冻结)
                  How to solve memory issues while multiprocessing using Pool.map()?(如何解决使用Pool.map()进行多处理时的内存问题?)
                  Python - How to use FastAPI and uvicorn.run without blocking the thread?(Python-如何使用FastAPI和uvicorn.run而不阻塞线程?)
                  How to increment a shared counter from multiple processes?(如何从多个进程递增共享计数器?)
                  Using pika, how to connect to rabbitmq running in docker, started with docker-compose with external network?(使用pika,如何连接运行在docker中的rabbitmq,从docker开始-与外部网络连接?)
                  How to use .rolling() on each row of a Pandas dataframe?(如何对 pandas 数据帧的每一行使用.roll()?)
                  <legend id='VnnsN'><style id='VnnsN'><dir id='VnnsN'><q id='VnnsN'></q></dir></style></legend>

                          <tfoot id='VnnsN'></tfoot>
                          • <bdo id='VnnsN'></bdo><ul id='VnnsN'></ul>

                            <i id='VnnsN'><tr id='VnnsN'><dt id='VnnsN'><q id='VnnsN'><span id='VnnsN'><b id='VnnsN'><form id='VnnsN'><ins id='VnnsN'></ins><ul id='VnnsN'></ul><sub id='VnnsN'></sub></form><legend id='VnnsN'></legend><bdo id='VnnsN'><pre id='VnnsN'><center id='VnnsN'></center></pre></bdo></b><th id='VnnsN'></th></span></q></dt></tr></i><div id='VnnsN'><tfoot id='VnnsN'></tfoot><dl id='VnnsN'><fieldset id='VnnsN'></fieldset></dl></div>
                          • <small id='VnnsN'></small><noframes id='VnnsN'>

                              <tbody id='VnnsN'></tbody>