<legend id='t7lHM'><style id='t7lHM'><dir id='t7lHM'><q id='t7lHM'></q></dir></style></legend>
      <tfoot id='t7lHM'></tfoot>
    1. <small id='t7lHM'></small><noframes id='t7lHM'>

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

        将项目附加到列表列表中的指定列表(Python)

        时间:2023-09-27
        • <bdo id='eU8qL'></bdo><ul id='eU8qL'></ul>

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

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

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

                    <tbody id='eU8qL'></tbody>
                  <tfoot id='eU8qL'></tfoot>
                  本文介绍了将项目附加到列表列表中的指定列表(Python)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我目前正在通过解决来自项目 euler 的问题来练习我的编程技能,现在我在 Python 上遇到了一些(在我看来)奇怪的行为.

                  I'm practicing my progamming skills by solving problems from project euler at the moment, and now I've come across some (in my opinion) strange behavior on Python.

                  当我这样做时:

                  list = [[1]]*20
                  

                  正如预期的那样,我得到了一个包含元素 1 的 20 个列表的列表.但是,当我想将 2 附加到此列表中的第三个元素时,我会这样做:

                  I get a list of 20 lists containing element 1, as expected. However, when I would like to append a 2 to the third element from this list, I would do that as follows:

                  list[3].append(2)
                  

                  然而,这会改变列表中的所有元素.即使我绕道而行,比如:

                  This however changes ALL the elements in the list. Even when I take a detour, like:

                  l = list[3]
                  l.append(2)
                  list[3] = l
                  

                  我所有的元素都变了.谁能告诉我如何执行此操作并获得如下输出:

                  All my elements get changed. Can anyone please tell me how to do this and get an output like so:

                  [[1], [1], [1], [1, 2], [1] .... [1]]
                  

                  提前致谢.

                  推荐答案

                  Python 列表是可变对象,所以当您执行 [[1]]*20 时,它会创建 one 列出对象[1],然后在顶层列表中放置20个对它的引用.

                  Python lists are mutable objects, so when you do [[1]]*20 it creates one list object [1] and then places 20 references to it in the toplevel list.

                  就可变性问题而言,和下面是一样的

                  As far as the mutability problem is concerned, this is the same as the following

                  a = [1,2,3]
                  b = a
                  b.append(4)
                  a # [1,2,3,4]
                  

                  这是因为 b=a 只是将 referencea 复制到 b 的列表实例.它们都指的是同一个实际列表.

                  This happens because b=a merely copies the reference to the list instance from a to b. They are both referring to the same actual list.

                  为了创建列表列表,就像您在上面尝试的那样,您需要为每个条目创建一个唯一列表.列表推导效果很好:

                  In order to create a list of lists, like you tried above, you need to create a unique list for each entry. A list comprehension works nicely:

                  mainlist = [[1] for x in range(20)]
                  mainlist[0].append(2)
                  mainlist # [[1,2],[1],[1],...]
                  

                  编辑

                  顺便说一句,由于类型名称是 Python 中的元类,因此使用类型名称命名变量是个坏主意.原因是这可能会导致代码中出现一些问题:

                  As an aside, since type names are metaclasses in Python, naming your variables by the type name is a bad idea. The reason is that can cause several issues further down in the code:

                  a = range(3) # [0,1,2]
                  type(a) # (type 'list')
                  isinstance(a, list) # True
                  

                  现在,创建一个名为 list

                  list = range(3)
                  list # [0,1,2]
                  isinstance(list, list)
                  Traceback (most recent call last):
                    File "<stdin>", line 1, in <module>
                  TypeError: isinstance() arg 2 must be a class, type, or tuple of classes and types
                  

                  更不用说,现在你不能使用 list() 操作符

                  Not to mention, now you cant use the list() operator

                  c = list((1,2,3))
                  Traceback (most recent call last):
                    File "<stdin>", line 1, in <module>
                  TypeError: 'list' object is not callable
                  

                  这篇关于将项目附加到列表列表中的指定列表(Python)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:在 Python 中将多个变量附加到列表中 下一篇:如何将项目附加到 Pandas 中不同列的列表中

                  相关文章

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

                      <legend id='93syn'><style id='93syn'><dir id='93syn'><q id='93syn'></q></dir></style></legend>

                    1. <small id='93syn'></small><noframes id='93syn'>