<small id='9BPvF'></small><noframes id='9BPvF'>

<tfoot id='9BPvF'></tfoot>

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

          <bdo id='9BPvF'></bdo><ul id='9BPvF'></ul>

        如何使用列表推导将元组的元组转换为一维列表?

        时间:2023-09-01

          <legend id='Xm3hm'><style id='Xm3hm'><dir id='Xm3hm'><q id='Xm3hm'></q></dir></style></legend><tfoot id='Xm3hm'></tfoot>

          • <bdo id='Xm3hm'></bdo><ul id='Xm3hm'></ul>
            • <small id='Xm3hm'></small><noframes id='Xm3hm'>

            • <i id='Xm3hm'><tr id='Xm3hm'><dt id='Xm3hm'><q id='Xm3hm'><span id='Xm3hm'><b id='Xm3hm'><form id='Xm3hm'><ins id='Xm3hm'></ins><ul id='Xm3hm'></ul><sub id='Xm3hm'></sub></form><legend id='Xm3hm'></legend><bdo id='Xm3hm'><pre id='Xm3hm'><center id='Xm3hm'></center></pre></bdo></b><th id='Xm3hm'></th></span></q></dt></tr></i><div id='Xm3hm'><tfoot id='Xm3hm'></tfoot><dl id='Xm3hm'><fieldset id='Xm3hm'></fieldset></dl></div>
                <tbody id='Xm3hm'></tbody>
                  本文介绍了如何使用列表推导将元组的元组转换为一维列表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我有一个元组 - 例如:

                  I have a tuple of tuples - for example:

                  tupleOfTuples = ((1, 2), (3, 4), (5,))
                  

                  我想将其转换为按顺序排列的所有元素的平面一维列表:

                  I want to convert this into a flat, one-dimensional list of all the elements in order:

                  [1, 2, 3, 4, 5]
                  

                  我一直在尝试通过列表理解来实现这一点.但我似乎无法弄清楚.我能够通过 for-each 循环来完成它:

                  I've been trying to accomplish this with list comprehension. But I can't seem to figure it out. I was able to accomplish it with a for-each loop:

                  myList = []
                  for tuple in tupleOfTuples:
                     myList = myList + list(tuple)
                  

                  但我觉得必须有一种方法可以通过列表理解来做到这一点.

                  But I feel like there must be a way to do this with a list comprehension.

                  一个简单的 [list(tuple) for tupleOfTuples] 只是给你一个列表列表,而不是单个元素.我想我也许可以通过使用解包运算符来解包列表,如下所示:

                  A simple [list(tuple) for tuple in tupleOfTuples] just gives you a list of lists, instead of individual elements. I thought I could perhaps build on this by using the unpacking operator to then unpack the list, like so:

                  [*list(tuple) for tuple in tupleOfTuples]
                  

                  [*(list(tuple)) for tuple in tupleOfTuples]
                  

                  ...但这没有用.有任何想法吗?还是我应该坚持循环?

                  ... but that didn't work. Any ideas? Or should I just stick to the loop?

                  推荐答案

                  通常称为扁平化嵌套结构.

                  it's typically referred to as flattening a nested structure.

                  >>> tupleOfTuples = ((1, 2), (3, 4), (5,))
                  >>> [element for tupl in tupleOfTuples for element in tupl]
                  [1, 2, 3, 4, 5]
                  

                  只是为了展示效率:

                  >>> import timeit
                  >>> it = lambda: list(chain(*tupleOfTuples))
                  >>> timeit.timeit(it)
                  2.1475738355700913
                  >>> lc = lambda: [element for tupl in tupleOfTuples for element in tupl]
                  >>> timeit.timeit(lc)
                  1.5745135182887857
                  

                  ETA:请不要使用 tuple 作为变量名,它会影响内置.

                  ETA: Please don't use tuple as a variable name, it shadows built-in.

                  这篇关于如何使用列表推导将元组的元组转换为一维列表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:在 Python 中查找元组列表中的最大值 下一篇:JSON序列化以元组为键的字典

                  相关文章

                • <tfoot id='j1qhW'></tfoot>
                • <legend id='j1qhW'><style id='j1qhW'><dir id='j1qhW'><q id='j1qhW'></q></dir></style></legend>

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

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