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()
这是因为 coord_list
正在存储 [xyz_list, xyz_list, ...]
。您在每次迭代中更新 xyz_list
,而迭代又在 coord_list
中更新。
问题出在访问托管堆的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']]
删除 del
并在将其添加到 coord_list
后清除列表 xyz_list
:
def coords() :
xyz_list = []
coord_list = []
while 1 :
xyz = raw_input("Enter co-ordinates (x,y,z) enter 'q' when done: ")
xyz = str(xyz)
for num in xyz.split(","):
xyz_list.append(num)
print xyz_list
if xyz[0] != 'q' :
coord_list.append(xyz_list)
print coord_list
xyz_list = []
else :
break
coords()
输出:
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: 4,5,6
['4', '5', '6']
[['1', '2', '3'], ['4', '5', '6']]
替换以下表达式:
del xyz_list[:]
与:
xyz_list = []
我创建了一个 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()
这是因为 coord_list
正在存储 [xyz_list, xyz_list, ...]
。您在每次迭代中更新 xyz_list
,而迭代又在 coord_list
中更新。
问题出在访问托管堆的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']]
删除 del
并在将其添加到 coord_list
后清除列表 xyz_list
:
def coords() :
xyz_list = []
coord_list = []
while 1 :
xyz = raw_input("Enter co-ordinates (x,y,z) enter 'q' when done: ")
xyz = str(xyz)
for num in xyz.split(","):
xyz_list.append(num)
print xyz_list
if xyz[0] != 'q' :
coord_list.append(xyz_list)
print coord_list
xyz_list = []
else :
break
coords()
输出:
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: 4,5,6
['4', '5', '6']
[['1', '2', '3'], ['4', '5', '6']]
替换以下表达式:
del xyz_list[:]
与:
xyz_list = []