列出故障?(Python)



我的列表有问题。很明显,我只是错过了一些东西P

有人能告诉我这里出了什么问题以及如何解决吗?以下是我出现故障的地方:

        On = [0, 0, [[0, 0],[0,1]]]
        tempList = []
        tempList.append(On[2])
        print(tempList)
        tempList.append([On[0],On[1]+1])
        print(tempList)

万一这很重要,那是为了我的AI寻路。

首次打印:

[[[[0, 0]], [0, 1]]]

我想要:

[[0,0],[0,1]]

第二次印刷:

[[[[0, 0]], [0, 1]], [0, 2]]

我想要:

[[0,0],[0,1],[0,2]]

On[2]应该追踪我过去的动作。我试图让我过去的动作(On[2](与当前的动作相结合。

我希望tempList是这样的:[[0,1],[0,2],[0,3]]

但我得到的却是:[[[0,1],[0,2]],[0,3]]

On以以下格式存储(或应该是(:[CurrentX,CurrentY,[[Step1X,Step1Y],[Step2X,Step2Y]]

如果你需要更多信息,告诉我你需要什么。

编辑:问题出在OntempList之间。

编辑2:如果你们需要,我可以发布所有代码,这样你们就可以运行它。:/

此行:

tempList.append([On[0],On[1]+1])

将列表附加到列表中。你想要这个:

tempList.extend([On[0], On[1] + 1])
On = [0, 1, [[0, 0],[0,1]]]
tempList = []
tempList.extend(On[2])
print(tempList)
tempList.append([On[0],On[1]+1]) # changing only this line
print(tempList)

收益率。。。

[[0, 0], [0, 1]]
[[0, 0], [0, 1], [0, 2]]

这是所述的期望结果。

如果您的Bottom显示为…

[0, 0, [[[0,1],[0,2],[0,3]]]]

当你希望它是…

[0, 0, [[0,1],[0,2],[0,3]]]

那么问题可能根本不在于tempList及其构造,而在于append调用,该调用将其参数附加为单个元素。


也就是说:

a=[1,2]
b=[3]
a.append(b)

结果…

a == [1,2,[3]]

而不是

a == [1,2,3]

我想这就是你真正想要的。


为此,请使用

a += b

a.extend(b)

最新更新