列出故障?(Python)

2024-10-04 03:28:40 发布

您现在位置:Python中文网/ 问答频道 /正文

我的单子有问题。 显然,我错过了一些东西。:页

有人能告诉我这里出了什么问题,怎么解决吗? 这里是我的故障:

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

以防万一这很重要,这是我的人工智能寻路。你知道吗

第一次打印:

[[[[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。你知道吗

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


Tags: on格式人工智能单子故障print动作append
3条回答
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]]

……这是所说的理想结果。你知道吗

这条线:

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

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

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

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

相关问题 更多 >