Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 3.x 当我将不同的列表附加到列表列表时,我只得到最后一个列表。为什么?_Python 3.x_List_Stack - Fatal编程技术网

Python 3.x 当我将不同的列表附加到列表列表时,我只得到最后一个列表。为什么?

Python 3.x 当我将不同的列表附加到列表列表时,我只得到最后一个列表。为什么?,python-3.x,list,stack,Python 3.x,List,Stack,我试图记录python标准函数append/extend等使用的过去列表组合: log = [[(3, 23), (5, 7), (6, 14), (3, 0), (11, 24), (3, 5), (20, 19), (16, 9), (19, 5)]] counter = 0 switch = False while switch == False: counter += 1 if counter > 5: switch = True le

我试图记录python标准函数append/extend等使用的过去列表组合:

log = [[(3, 23), (5, 7), (6, 14), (3, 0), (11, 24), (3, 5), (20, 19), (16, 9), (19, 5)]]
counter = 0
switch = False

while switch == False:
    counter += 1

    if counter > 5:
        switch = True

    lenth = len(log)
    netlist = log[0]

    index = netlist.index((11, 24))

    tmp = netlist[index - 1]
    netlist[index - 1] = netlist[index]
    netlist[index] = tmp
    print(netlist)

    log.append(netlist)


print()
for lists in log:
    print(lists)
结果应该是一个日志变量,其中包含切换后使用的所有列表。因此,从日志中的de list开始,在每次切换之后,循环结束时,它应该将列表附加到日志中。所有使用的列表都应该打印出来

我得到的结果是一个包含所有相同列表(最后一个使用的列表)的日志。


第一个列表是它应该如何,最后一个列表是它现在如何。

有一些误解,我将试图澄清

误解1-列表是不可变的 我得到的结果是一个包含所有相同列表(最后一个使用的列表)的日志

正确理解-列表是可变的 使用以下代码交换列表中的值时-

tmp = netlist[index - 1]
netlist[index - 1] = netlist[index]
netlist[index] = tmp
您不是在创建新列表,而是在修改相同的列表

阅读列表和易变性

这方面的问题如下所示-

您的日志列表最终将包含对相同对象的多个引用

所以情况是这样的

         __________________________________________________
        |                  netlist                         |
        |                                                  |
        |                  value -                         |
        |  [(3, 23), (5, 7), (6, 14), (3, 0),              |
        |   (11, 24), (3, 5), (20, 19), (16, 9), (19, 5)]  |
        |                                                  |
        |                                                  |
        |                                                  |
        |_____^________________^______________^____________|
        ______|________________|______________|_________________
       |      |                |              |                 |
       |   ___|_____       ____|_____     ____|_____            |
       |  |         |     |          |   |          |           |
       |  |   0     |     |    1     |   |    2     |           |
       |  |_________|     |__________|   |__________|  ........ |
       |                                                        |
       |                  log                                   |
       |________________________________________________________|
如您所见,
0th
索引指的是与
1st
索引、
2nd
索引等相同的元素

您可能想要的是一个包含多个不同列表的
日志。
诸如此类-

         _____________     ____________     _____________
        |             |   |            |   |             |
        | [(3, 23),   |   |  another   |   | yet another |
        |   (5, 7)...]|   |    list    |   | list        |
        |             |   |            |   |             |
        |             |   |            |   |             |
        |_____^_______|   |____^_______|   |____^________| .......
        ______|________________|________________|_______________
       |      |                |                |               |
       |   ___|_____       ____|_____       ____|_____          |
       |  |         |     |          |     |          |         |
       |  |   0     |     |    1     |     |    2     |         |
       |  |_________|     |__________|     |__________| ....... |
       |                                                        |
       |                  log                                   |
       |________________________________________________________|
因此,当您尝试打印日志时,您得到的是一次又一次打印的相同列表

解决方法是使用
操作符复制初始列表。 阅读如何复制列表

误解2-使用
0
选择最后一个元素。 正确理解-使用
-1
获取最后一个元素。 这段代码-

netlist = log[0]
建议您尝试选择列表的最后一个元素,但使用索引
0

要获取列表中的最后一个元素,请使用索引
-1
。 了解切片表示法

最终代码- 这将打印以下输出,这是您所期望的

[(3, 23), (5, 7), (6, 14), (11, 24), (3, 0), (3, 5), (20, 19), (16, 9), (19, 5)]
[(3, 23), (5, 7), (11, 24), (6, 14), (3, 0), (3, 5), (20, 19), (16, 9), (19, 5)]
[(3, 23), (11, 24), (5, 7), (6, 14), (3, 0), (3, 5), (20, 19), (16, 9), (19, 5)]
[(11, 24), (3, 23), (5, 7), (6, 14), (3, 0), (3, 5), (20, 19), (16, 9), (19, 5)]
[(19, 5), (3, 23), (5, 7), (6, 14), (3, 0), (3, 5), (20, 19), (16, 9), (11, 24)]
[(19, 5), (3, 23), (5, 7), (6, 14), (3, 0), (3, 5), (20, 19), (11, 24), (16, 9)]
[(3, 23), (5, 7), (6, 14), (11, 24), (3, 0), (3, 5), (20, 19), (16, 9), (19, 5)]
[(3, 23), (5, 7), (11, 24), (6, 14), (3, 0), (3, 5), (20, 19), (16, 9), (19, 5)]
[(3, 23), (11, 24), (5, 7), (6, 14), (3, 0), (3, 5), (20, 19), (16, 9), (19, 5)]
[(11, 24), (3, 23), (5, 7), (6, 14), (3, 0), (3, 5), (20, 19), (16, 9), (19, 5)]
[(19, 5), (3, 23), (5, 7), (6, 14), (3, 0), (3, 5), (20, 19), (16, 9), (11, 24)]
[(19, 5), (3, 23), (5, 7), (6, 14), (3, 0), (3, 5), (20, 19), (11, 24), (16, 9)]