list.append()似乎无法正常工作(list.append() does not appear to work correctly)

编程入门 行业动态 更新时间:2024-10-28 02:34:16
list.append()似乎无法正常工作(list.append() does not appear to work correctly)

我创建了一个python函数,它应该采用一系列3D坐标并将它们放在一个列表中(在列表中)。

但是,当我打印出coord_list ,它似乎没有正确追加,例如输入这些坐标时:

[1,2,3] [2,3,4] [3,4,5]

coord_list的输出最终(忽略'q')将是: [[3,4,5],[3,4,5],[3,4,5]] 。

为什么它没有正确追加,如何解决?

def coords() : xyz_list = [] coord_list = [] while 1 : xyz = raw_input("Enter co-ordinates (x,y,z) enter 'q' when done: ") xyz = str(xyz) del xyz_list[:] for num in xyz.split(","): xyz_list.append(num) print xyz_list if xyz[0] != 'q' : coord_list.append(xyz_list) print coord_list else : break coords()

I have created a python function, which should take a series of 3D co-ordinates and place them in a list (within a list).

However when I print out coord_list, it doesn't appear to append correctly, for example when these co-ordinates are entered :

[1,2,3] [2,3,4] [3,4,5]

The output of coord_list finally (ignoring the 'q') will be: [[3,4,5],[3,4,5],[3,4,5]].

Why does it not append correctly, and how can this be fixed?

def coords() : xyz_list = [] coord_list = [] while 1 : xyz = raw_input("Enter co-ordinates (x,y,z) enter 'q' when done: ") xyz = str(xyz) del xyz_list[:] for num in xyz.split(","): xyz_list.append(num) print xyz_list if xyz[0] != 'q' : coord_list.append(xyz_list) print coord_list else : break coords()

最满意答案

问题在于访问托管堆的del 。 新对象( xyz_list的成员)出现在同一位置,因为不会删除包含列表。 因此,列表成员就地替换先前的列表成员, coord_list的引用将指向新值。

python 2.7.9(Linux)中的再现:

$ python coords.py Enter co-ordinates (x,y,z) enter 'q' when done: 1,2,3 ['1', '2', '3'] [['1', '2', '3']] Enter co-ordinates (x,y,z) enter 'q' when done: 2,3,4 ['2', '3', '4'] [['2', '3', '4'], ['2', '3', '4']] Enter co-ordinates (x,y,z) enter 'q' when done: 3,4,5 ['3', '4', '5'] [['3', '4', '5'], ['3', '4', '5'], ['3', '4', '5']]

我对脚本做了一个小改动: del xyz_list[:] - > xyz_list = [] 。

现在它有效:

$ python coords.py Enter co-ordinates (x,y,z) enter 'q' when done: 1,2,3 ['1', '2', '3'] [['1', '2', '3']] Enter co-ordinates (x,y,z) enter 'q' when done: 2,3,4 ['2', '3', '4'] [['1', '2', '3'], ['2', '3', '4']] Enter co-ordinates (x,y,z) enter 'q' when done: 3,4,5 ['3', '4', '5'] [['1', '2', '3'], ['2', '3', '4'], ['3', '4', '5']]

The problem lies in the del which accesses the managed heap. New objects (the members of xyz_list) appear on the same spot because the containing list is not deleted. So the list members replace the previous ones in-place and the reference in coord_list will point to the new values.

Reproduction in python 2.7.9 (Linux):

$ python coords.py Enter co-ordinates (x,y,z) enter 'q' when done: 1,2,3 ['1', '2', '3'] [['1', '2', '3']] Enter co-ordinates (x,y,z) enter 'q' when done: 2,3,4 ['2', '3', '4'] [['2', '3', '4'], ['2', '3', '4']] Enter co-ordinates (x,y,z) enter 'q' when done: 3,4,5 ['3', '4', '5'] [['3', '4', '5'], ['3', '4', '5'], ['3', '4', '5']]

I made a small change to the script: del xyz_list[:] --> xyz_list = [].

Now it works:

$ python coords.py Enter co-ordinates (x,y,z) enter 'q' when done: 1,2,3 ['1', '2', '3'] [['1', '2', '3']] Enter co-ordinates (x,y,z) enter 'q' when done: 2,3,4 ['2', '3', '4'] [['1', '2', '3'], ['2', '3', '4']] Enter co-ordinates (x,y,z) enter 'q' when done: 3,4,5 ['3', '4', '5'] [['1', '2', '3'], ['2', '3', '4'], ['3', '4', '5']]

更多推荐

本文发布于:2023-07-26 17:50:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1278939.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:无法正常   工作   append   list   work

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!