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

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

      alternately appending elements from two lists(交替附加两个列表中的元素)

    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
                  

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

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

                相关文档推荐

                Adding config modes to Plotly.Py offline - modebar(将配置模式添加到 Plotly.Py 离线 - 模式栏)
                Plotly: How to style a plotly figure so that it doesn#39;t display gaps for missing dates?(Plotly:如何设置绘图图形的样式,使其不显示缺失日期的间隙?)
                python save plotly plot to local file and insert into html(python将绘图保存到本地文件并插入到html中)
                Plotly: What color cycle does plotly express follow?(情节:情节表达遵循什么颜色循环?)
                How to save plotly express plot into a html or static image file?(如何将情节表达图保存到 html 或静态图像文件中?)
                Plotly: How to make a line plot from a pandas dataframe with a long or wide format?(Plotly:如何使用长格式或宽格式的 pandas 数据框制作线图?)
                  <bdo id='6tmhZ'></bdo><ul id='6tmhZ'></ul>
                    <tbody id='6tmhZ'></tbody>

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