列表故障? (Python)
List Malfunction? (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]]
等
如果您需要更多信息,请告诉我您需要什么。
编辑:问题出在 On
和 tempList
。
EDIT2:如果你们需要,我可以 post 所有代码,这样你们就可以 运行 了。 :/
这一行:
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)
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]]
...这是声明的期望结果。
我的列表有问题。 显然,我刚刚错过了一些东西。 :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]]
等
如果您需要更多信息,请告诉我您需要什么。
编辑:问题出在 On
和 tempList
。
EDIT2:如果你们需要,我可以 post 所有代码,这样你们就可以 运行 了。 :/
这一行:
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)
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]]
...这是声明的期望结果。