<legend id='j22aP'><style id='j22aP'><dir id='j22aP'><q id='j22aP'></q></dir></style></legend>

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

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

        尝试在 Python 中扩展列表时出现类型错误

        时间:2023-09-27
        • <legend id='8i2Rr'><style id='8i2Rr'><dir id='8i2Rr'><q id='8i2Rr'></q></dir></style></legend>

          <small id='8i2Rr'></small><noframes id='8i2Rr'>

            <tbody id='8i2Rr'></tbody>
          <tfoot id='8i2Rr'></tfoot>

            <bdo id='8i2Rr'></bdo><ul id='8i2Rr'></ul>

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

                1. 本文介绍了尝试在 Python 中扩展列表时出现类型错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我需要明白为什么:

                  years = range(2010,2016)
                  years.append(0)
                  

                  是可能的,返回:

                  [2010,2011,2012,2013,2014,2015,0]
                  

                  years = range(2010,2016).append(0)
                  

                  years = [0].extend(range(2010,2016))
                  

                  不工作?

                  我知道这是我收到的消息中的类型错误.但我想在这背后有更多的解释.

                  I understand that it is a type error from the message I got. But I'd like to have a bit more explanations behind that.

                  推荐答案

                  你正在存储 list.append()list.extend() 方法的结果;两者都更改列表就地并返回None.他们确实不会再次返回列表对象.

                  You are storing the result of the list.append() or list.extend() method; both alter the list in place and return None. They do not return the list object again.

                  不存储 None 结果;存储 range() 结果,then 扩展或追加.或者,使用串联:

                  Do not store the None result; store the range() result, then extend or append. Alternatively, use concatenation:

                  years = range(2010, 2016) + [0]
                  years = [0] + range(2010, 2016)
                  

                  请注意,我假设您使用的是 Python 2(否则您的第一个示例将无法正常工作).在 Python 3 中 range() 不会产生列表;您必须使用 list() 函数将其转换为一个:

                  Note that I'm assuming you are using Python 2 (your first example would not work otherwise). In Python 3 range() doesn't produce a list; you'd have to use the list() function to convert it to one:

                  years = list(range(2010, 2016)) + [0]
                  years = [0] + list(range(2010, 2016))
                  

                  这篇关于尝试在 Python 中扩展列表时出现类型错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:从数据框的开头向 pandas 数据框的末尾添加值 下一篇:Python,比较子列表和制作列表

                  相关文章

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

                  <legend id='Bjwra'><style id='Bjwra'><dir id='Bjwra'><q id='Bjwra'></q></dir></style></legend>

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

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