<i id='Ex7Sb'><tr id='Ex7Sb'><dt id='Ex7Sb'><q id='Ex7Sb'><span id='Ex7Sb'><b id='Ex7Sb'><form id='Ex7Sb'><ins id='Ex7Sb'></ins><ul id='Ex7Sb'></ul><sub id='Ex7Sb'></sub></form><legend id='Ex7Sb'></legend><bdo id='Ex7Sb'><pre id='Ex7Sb'><center id='Ex7Sb'></center></pre></bdo></b><th id='Ex7Sb'></th></span></q></dt></tr></i><div id='Ex7Sb'><tfoot id='Ex7Sb'></tfoot><dl id='Ex7Sb'><fieldset id='Ex7Sb'></fieldset></dl></div>
      1. <legend id='Ex7Sb'><style id='Ex7Sb'><dir id='Ex7Sb'><q id='Ex7Sb'></q></dir></style></legend>
      2. <small id='Ex7Sb'></small><noframes id='Ex7Sb'>

        <tfoot id='Ex7Sb'></tfoot>
          <bdo id='Ex7Sb'></bdo><ul id='Ex7Sb'></ul>

        在 Python 中展平一个浅列表

        Flattening a shallow list in Python(在 Python 中展平一个浅列表)
            <tbody id='0Ct1G'></tbody>
          <tfoot id='0Ct1G'></tfoot>
        • <i id='0Ct1G'><tr id='0Ct1G'><dt id='0Ct1G'><q id='0Ct1G'><span id='0Ct1G'><b id='0Ct1G'><form id='0Ct1G'><ins id='0Ct1G'></ins><ul id='0Ct1G'></ul><sub id='0Ct1G'></sub></form><legend id='0Ct1G'></legend><bdo id='0Ct1G'><pre id='0Ct1G'><center id='0Ct1G'></center></pre></bdo></b><th id='0Ct1G'></th></span></q></dt></tr></i><div id='0Ct1G'><tfoot id='0Ct1G'></tfoot><dl id='0Ct1G'><fieldset id='0Ct1G'></fieldset></dl></div>

            1. <small id='0Ct1G'></small><noframes id='0Ct1G'>

                <legend id='0Ct1G'><style id='0Ct1G'><dir id='0Ct1G'><q id='0Ct1G'></q></dir></style></legend>
                  <bdo id='0Ct1G'></bdo><ul id='0Ct1G'></ul>

                  本文介绍了在 Python 中展平一个浅列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  有没有一种简单的方法可以通过列表推导来展平可迭代列表,或者如果做不到这一点,你们都认为什么是展平这样的浅层列表、平衡性能和可读性的最佳方法?

                  Is there a simple way to flatten a list of iterables with a list comprehension, or failing that, what would you all consider to be the best way to flatten a shallow list like this, balancing performance and readability?

                  我尝试使用嵌套列表理解来展平这样的列表,如下所示:

                  I tried to flatten such a list with a nested list comprehension, like this:

                  [image for image in menuitem for menuitem in list_of_menuitems]
                  

                  但是我遇到了 NameError 种类的问题,因为 name 'menuitem' 没有定义.在谷歌上搜索并查看 Stack Overflow 后,我通过 reduce 语句得到了想要的结果:

                  But I get in trouble of the NameError variety there, because the name 'menuitem' is not defined. After googling and looking around on Stack Overflow, I got the desired results with a reduce statement:

                  reduce(list.__add__, map(lambda x: list(x), list_of_menuitems))
                  

                  但是这个方法相当不可读,因为我需要在那里调用 list(x) 因为 x 是一个 Django QuerySet 对象.

                  But this method is fairly unreadable because I need that list(x) call there because x is a Django QuerySet object.

                  结论:

                  感谢所有为这个问题做出贡献的人.这里是我学到的总结.如果其他人想要添加或更正这些观察结果,我也会将此作为社区 wiki.

                  Thanks to everyone who contributed to this question. Here is a summary of what I learned. I'm also making this a community wiki in case others want to add to or correct these observations.

                  我原来的reduce语句是多余的,最好这样写:

                  My original reduce statement is redundant and is better written this way:

                  >>> reduce(list.__add__, (list(mi) for mi in list_of_menuitems))
                  

                  这是嵌套列表理解的正确语法(精彩总结dF!):

                  This is the correct syntax for a nested list comprehension (Brilliant summary dF!):

                  >>> [image for mi in list_of_menuitems for image in mi]
                  

                  但是这些方法都没有使用 itertools.chain 高效:

                  But neither of these methods are as efficient as using itertools.chain:

                  >>> from itertools import chain
                  >>> list(chain(*list_of_menuitems))
                  

                  正如@cdleary 所指出的,使用 chain.from_iterable 来避免 * 运算符魔法可能是更好的风格,如下所示:

                  And as @cdleary notes, it's probably better style to avoid * operator magic by using chain.from_iterable like so:

                  >>> chain = itertools.chain.from_iterable([[1,2],[3],[5,89],[],[6]])
                  >>> print(list(chain))
                  >>> [1, 2, 3, 5, 89, 6]
                  

                  推荐答案

                  如果您只是想迭代数据结构的扁平化版本并且不需要可索引的序列,请考虑 itertools.chain 和公司.

                  If you're just looking to iterate over a flattened version of the data structure and don't need an indexable sequence, consider itertools.chain and company.

                  >>> list_of_menuitems = [['image00', 'image01'], ['image10'], []]
                  >>> import itertools
                  >>> chain = itertools.chain(*list_of_menuitems)
                  >>> print(list(chain))
                  ['image00', 'image01', 'image10']
                  

                  它适用于任何可迭代的东西,其中应包括 Django 的可迭代 QuerySet,您似乎在问题中使用了它.

                  It will work on anything that's iterable, which should include Django's iterable QuerySets, which it appears that you're using in the question.

                  这可能与 reduce 一样好,因为 reduce 将具有相同的开销将项目复制到正在扩展的列表中.chain 只有在最后运行 list(chain) 时才会产生这种(相同的)开销.

                  This is probably as good as a reduce anyway, because reduce will have the same overhead copying the items into the list that's being extended. chain will only incur this (same) overhead if you run list(chain) at the end.

                  实际上,它的开销比问题提出的解决方案要少,因为当您使用临时列表扩展原始列表时,您会丢弃创建的临时列表.

                  Meta- Actually, it's less overhead than the question's proposed solution, because you throw away the temporary lists you create when you extend the original with the temporary.

                  作为 J.F.Sebastian 说 itertools.chain.from_iterable 避免了解包,你应该使用它来避免 * 魔法,但是 timeit 应用 的性能差异可以忽略不计.

                  As J.F. Sebastian says itertools.chain.from_iterable avoids the unpacking and you should use that to avoid * magic, but the timeit app shows negligible performance difference.

                  这篇关于在 Python 中展平一个浅列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

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

                  相关文档推荐

                  Split a Pandas column of lists into multiple columns(将 Pandas 的列表列拆分为多列)
                  How does the @property decorator work in Python?(@property 装饰器在 Python 中是如何工作的?)
                  What is the difference between old style and new style classes in Python?(Python中的旧样式类和新样式类有什么区别?)
                  How to break out of multiple loops?(如何打破多个循环?)
                  How to put the legend out of the plot(如何将传说从情节中剔除)
                  Why is the output of my function printing out quot;Nonequot;?(为什么我的函数输出打印出“无?)

                    <bdo id='g3hhl'></bdo><ul id='g3hhl'></ul>

                        <i id='g3hhl'><tr id='g3hhl'><dt id='g3hhl'><q id='g3hhl'><span id='g3hhl'><b id='g3hhl'><form id='g3hhl'><ins id='g3hhl'></ins><ul id='g3hhl'></ul><sub id='g3hhl'></sub></form><legend id='g3hhl'></legend><bdo id='g3hhl'><pre id='g3hhl'><center id='g3hhl'></center></pre></bdo></b><th id='g3hhl'></th></span></q></dt></tr></i><div id='g3hhl'><tfoot id='g3hhl'></tfoot><dl id='g3hhl'><fieldset id='g3hhl'></fieldset></dl></div>
                        1. <tfoot id='g3hhl'></tfoot>
                            <tbody id='g3hhl'></tbody>
                          <legend id='g3hhl'><style id='g3hhl'><dir id='g3hhl'><q id='g3hhl'></q></dir></style></legend>

                            <small id='g3hhl'></small><noframes id='g3hhl'>