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

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

  2. <tfoot id='tVRAH'></tfoot>

      我可以在 Python 中使用前置元素而不是附加来扩展列表吗?

      Can I extend list in Python with prepend elements instead of append?(我可以在 Python 中使用前置元素而不是附加来扩展列表吗?)
      <legend id='9gC2S'><style id='9gC2S'><dir id='9gC2S'><q id='9gC2S'></q></dir></style></legend>

        • <bdo id='9gC2S'></bdo><ul id='9gC2S'></ul>

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

              1. <i id='9gC2S'><tr id='9gC2S'><dt id='9gC2S'><q id='9gC2S'><span id='9gC2S'><b id='9gC2S'><form id='9gC2S'><ins id='9gC2S'></ins><ul id='9gC2S'></ul><sub id='9gC2S'></sub></form><legend id='9gC2S'></legend><bdo id='9gC2S'><pre id='9gC2S'><center id='9gC2S'></center></pre></bdo></b><th id='9gC2S'></th></span></q></dt></tr></i><div id='9gC2S'><tfoot id='9gC2S'></tfoot><dl id='9gC2S'><fieldset id='9gC2S'></fieldset></dl></div>
                  <tbody id='9gC2S'></tbody>
                <tfoot id='9gC2S'></tfoot>
                本文介绍了我可以在 Python 中使用前置元素而不是附加来扩展列表吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                I can perform

                a = [1,2,3]
                b = [4,5,6]
                a.extend(b)
                # a is now [1,2,3,4,5,6]
                

                Is there way to perform an action for extending list and adding new items to the beginning of the list?

                Like this

                a = [1,2,3]
                b = [4,5,6]
                a.someaction(b)
                # a is now [4,5,6,1,2,3]
                

                I use version 2.7.5, if it is important.

                解决方案

                You can assign to a slice:

                a[:0] = b
                

                Demo:

                >>> a = [1,2,3]
                >>> b = [4,5,6]
                >>> a[:0] = b
                >>> a
                [4, 5, 6, 1, 2, 3]
                

                Essentially, list.extend() is an assignment to the list[len(list):] slice.

                You can 'insert' another list at any position, just address the empty slice at that location:

                >>> a = [1,2,3]
                >>> b = [4,5,6]
                >>> a[1:1] = b
                >>> a
                [1, 4, 5, 6, 2, 3]
                

                这篇关于我可以在 Python 中使用前置元素而不是附加来扩展列表吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

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

                相关文档推荐

                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='C3Fd8'></bdo><ul id='C3Fd8'></ul>
                  • <i id='C3Fd8'><tr id='C3Fd8'><dt id='C3Fd8'><q id='C3Fd8'><span id='C3Fd8'><b id='C3Fd8'><form id='C3Fd8'><ins id='C3Fd8'></ins><ul id='C3Fd8'></ul><sub id='C3Fd8'></sub></form><legend id='C3Fd8'></legend><bdo id='C3Fd8'><pre id='C3Fd8'><center id='C3Fd8'></center></pre></bdo></b><th id='C3Fd8'></th></span></q></dt></tr></i><div id='C3Fd8'><tfoot id='C3Fd8'></tfoot><dl id='C3Fd8'><fieldset id='C3Fd8'></fieldset></dl></div>
                    <tfoot id='C3Fd8'></tfoot>
                      • <small id='C3Fd8'></small><noframes id='C3Fd8'>

                        <legend id='C3Fd8'><style id='C3Fd8'><dir id='C3Fd8'><q id='C3Fd8'></q></dir></style></legend>
                            <tbody id='C3Fd8'></tbody>