【发布时间】:2016-10-29 03:26:14
【问题描述】:
我正在努力找出这里出了什么问题我正在尝试将此列表附加到字典键的列表中,但我只得到最后一个。
例如:
pastmoves =['n','w','s','w']
moves = [1,0,1,0]
turnpt = {'pos' : [],
'moves' : [],
'lastmove' : []}
pos = [1,1]
opt = [1]
while 5 not in opt:
if len(pastmoves) > 1:
if moves.count(1) > 1:
if pos not in turnpt['pos']:
turnpt['pos'].append(pos)
print(turnpt['pos'])
pos[1] += 1
print(pos)
opt[0] += 1
else:
print(opt)
我的标准输出显示:
[[1, 1]]
[1, 2]
[1, 3]
[1, 4]
[1, 5]
[5]
我希望pos 的每个版本都被附加到turnpt['pos'] 列表中,但这并没有发生,这是为什么呢?
注意:
我的 if 逻辑是嵌套的,因为我需要在每个操作之间完成其他操作,这只是一个工作示例。
【问题讨论】:
-
你的缩进不正确
标签: python list dictionary while-loop append