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

<tfoot id='HXvkI'></tfoot>
  1. <legend id='HXvkI'><style id='HXvkI'><dir id='HXvkI'><q id='HXvkI'></q></dir></style></legend>

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

      Python append() 与列表上的 + 运算符,为什么这些会给出不同的结果?

      Python append() vs. + operator on lists, why do these give different results?(Python append() 与列表上的 + 运算符,为什么这些会给出不同的结果?)

        • <legend id='d4VuB'><style id='d4VuB'><dir id='d4VuB'><q id='d4VuB'></q></dir></style></legend>

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

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

                <tfoot id='d4VuB'></tfoot>
                  <tbody id='d4VuB'></tbody>
                本文介绍了Python append() 与列表上的 + 运算符,为什么这些会给出不同的结果?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                为什么这两个操作(append()+)会给出不同的结果?

                Why do these two operations (append() resp. +) give different results?

                >>> c = [1, 2, 3]
                >>> c
                [1, 2, 3]
                >>> c += c
                >>> c
                [1, 2, 3, 1, 2, 3]
                >>> c = [1, 2, 3]
                >>> c.append(c)
                >>> c
                [1, 2, 3, [...]]
                >>> 
                

                在最后一种情况下,实际上存在无限递归.c[-1]c 是一样的.为什么与 + 操作不同?

                In the last case there's actually an infinite recursion. c[-1] and c are the same. Why is it different with the + operation?

                推荐答案

                解释为什么":

                + 操作添加array 元素到原始数组.array.append 操作将数组(或任何对象)插入到原始数组的末尾,这会导致在该位置对 self 的引用(因此是无限递归).

                To explain "why":

                The + operation adds the array elements to the original array. The array.append operation inserts the array (or any object) into the end of the original array, which results in a reference to self in that spot (hence the infinite recursion).

                这里的区别在于 + 操作在您添加数组时执行特定操作(它像其他操作一样重载,请参阅 本章 序列)通过连接元素.然而,附加方法确实按照您的要求执行:将对象附加到您给它的右侧(数组或任何其他对象),而不是获取它的元素.

                The difference here is that the + operation acts specific when you add an array (it's overloaded like others, see this chapter on sequences) by concatenating the element. The append-method however does literally what you ask: append the object on the right-hand side that you give it (the array or any other object), instead of taking its elements.

                使用 extend() 如果您想使用类似于 + 运算符的功能(正如其他人在此处显示的那样).做相反的事情是不明智的:尝试用 + 运算符模拟列表的追加(请参阅我的 早期链接为什么).

                为了好玩,有一点历史:诞生1993 年 2 月 Python 中的数组模块.它可能会让你感到惊讶,但数组是在序列和列表出现之后添加的.

                For fun, a little history: the birth of the array module in Python in February 1993. it might surprise you, but arrays were added way after sequences and lists came into existence.

                这篇关于Python append() 与列表上的 + 运算符,为什么这些会给出不同的结果?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

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

                相关文档推荐

                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 数据框制作线图?)

                  • <tfoot id='nXKsu'></tfoot>

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

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

                          <tbody id='nXKsu'></tbody>
                          <bdo id='nXKsu'></bdo><ul id='nXKsu'></ul>