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

        <tfoot id='TSazj'></tfoot>
      1. 列表 += 元组与列表 = 列表 + 元组

        时间:2023-08-31

            <bdo id='HNgI9'></bdo><ul id='HNgI9'></ul>

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

                <tbody id='HNgI9'></tbody>

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

                  本文介绍了列表 += 元组与列表 = 列表 + 元组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  假设我有这些任务:

                  points = []
                  point = (1, 2)
                  

                  我怎么会这样做:

                  points += point
                  

                  它工作得很好,并且给了我点 = [1, 2].但是,如果我这样做:

                  It works completely fine, and gives me points = [1, 2]. However, If I do something like:

                  points = points + point
                  

                  它给了我一个 TypeError: can only concatenate list (not "tuple") to list.不过,这些陈述不是一回事吗?

                  It gives me a TypeError: can only concatenate list (not "tuple") to list. Aren't these statements the same thing, though?

                  推荐答案

                  不同的是,list +=等价于list.extend(),取任何可迭代并扩展列表,它作为元组工作是可迭代的.(并就地扩展列表).

                  The difference, is that list += is equivalent to list.extend(), which takes any iterable and extends the list, it works as a tuple is an iterable. (And extends the list in-place).

                  另一方面,第二个将一个新列表分配给 points,并尝试将一个列表连接到一个元组,但由于不清楚预期结果是什么(列表或元组?).

                  On the other hand, the second assigns a new list to points, and attempts to concatenate a list to a tuple, which isn't done as it's unclear what the expected results is (list or tuple?).

                  这篇关于列表 += 元组与列表 = 列表 + 元组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:python 元组是不可变的 - 那为什么我可以向它添加元素 下一篇:python中将列表转换为元组的时间复杂度,反之亦然

                  相关文章

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

                    1. <tfoot id='ZkN2O'></tfoot>

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

                        <bdo id='ZkN2O'></bdo><ul id='ZkN2O'></ul>