gpt4 book ai didi

python - list.append() 似乎无法正常工作

转载 作者:太空宇宙 更新时间:2023-11-04 10:18:46 25 4
gpt4 key购买 nike

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

然而,当我打印出 coord_list 时,它似乎没有正确 append ,例如当输入这些坐标时:

[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 中更新。

关于python - list.append() 似乎无法正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33631987/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com