列表故障? (蟒蛇)

时间:2015-05-20 00:43:57

标签: python list artificial-intelligence

我的列表出了问题。 显然,我错过了一些东西。 :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

EDIT2:如果您需要,我可以发布所有代码,以便您可以运行它。 :/

3 个答案:

答案 0 :(得分:2)

这一行:

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

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

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

答案 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]]

...这是所要求的预期结果。

答案 2 :(得分:0)

如果您的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)