我创建了一个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']]更多推荐
发布评论