<bdo id='jdSNf'></bdo><ul id='jdSNf'></ul>
  1. <legend id='jdSNf'><style id='jdSNf'><dir id='jdSNf'><q id='jdSNf'></q></dir></style></legend>

  2. <small id='jdSNf'></small><noframes id='jdSNf'>

    <tfoot id='jdSNf'></tfoot>

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

      交替附加两个列表中的元素

      时间:2023-09-27

    1. <small id='aRhMj'></small><noframes id='aRhMj'>

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

            <legend id='aRhMj'><style id='aRhMj'><dir id='aRhMj'><q id='aRhMj'></q></dir></style></legend>
              <bdo id='aRhMj'></bdo><ul id='aRhMj'></ul>
              <tfoot id='aRhMj'></tfoot>
                  <tbody id='aRhMj'></tbody>
              • 本文介绍了交替附加两个列表中的元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                I have three lists with elements :

                a = [[0,1],[2,3],...]
                b = [[5,6],[7,8],...]
                
                c = []
                

                I want to append elements from a and b into c to get:

                c = [ [0,1],[5,6],[2,3],[7,8],.... ]
                

                解决方案

                Another very simple approach using string slicing (and most performance efficient) as:

                >>> a = [[0,1],[2,3]]
                >>> b = [[5,6],[7,8]]
                >>> c = a + b # create a list with size = len(a) + len(b)
                >>> c[::2], c[1::2] = a, b  # alternately insert the value
                >>> c
                [[0, 1], [5, 6], [2, 3], [7, 8]]
                

                Below is the comparison of results with timeit for the answers mentioned here (Python version: 2.7):

                1. Using string slicing: 0.586 usec per loop

                  moin@moin-pc:~$ python -m "timeit" -s "a = [[0,1],[2,3]]; b = [[5,6],[7,8]];" "c = a + b; c[::2], c[1::2] = a, b"
                  1000000 loops, best of 3: 0.586 usec per loop
                  

                2. Using itertools.chain(): 1.89 usec per loop

                  moin@moin-pc:~$ python -m "timeit" -s "from itertools import chain; a = [[0,1],[2,3]]; b = [[5,6],[7,8]];" "c = list(chain(*zip(a, b)))"
                  1000000 loops, best of 3: 1.89 usec per loop
                  

                3. Using reduce(): 0.829 usec per loop

                  moin@moin-pc:~$ python -m "timeit" -s "import operator; a = [[0,1],[2,3]]; b = [[5,6],[7,8]];" "c = reduce(operator.concat, zip(a, b))"
                  1000000 loops, best of 3: 0.829 usec per loop
                  

                4. Using list.extend(): 0.824 usec per loop

                   moin@moin-pc:~$ python -m "timeit" -s "a = [[0,1],[2,3]]; b = [[5,6],[7,8]]; c=[]" "for pair in zip(a,b): c.extend(pair)"
                   1000000 loops, best of 3: 0.824 usec per loop
                  

                5. Using list.append() twice: 1.04 usec per loop

                  moin@moin-pc:~$ python -m "timeit" -s "a = [[0,1],[2,3]]; b = [[5,6],[7,8]]; c=[]" "for a_element, b_element in zip(a, b): c.append(a_element); c.append(b_element)"
                  1000000 loops, best of 3: 1.04 usec per loop
                  

                这篇关于交替附加两个列表中的元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                上一篇:将函数输出附加到多个列表的 Pythonic 方法 下一篇:附加到覆盖先前值的元组

                相关文章

                  <bdo id='6tmhZ'></bdo><ul id='6tmhZ'></ul>

                    <small id='6tmhZ'></small><noframes id='6tmhZ'>

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

                    <legend id='6tmhZ'><style id='6tmhZ'><dir id='6tmhZ'><q id='6tmhZ'></q></dir></style></legend>