在我的游戏中,我有一个元组列表 (x,y) :
solution = [(36, 37), (36, 36), (36, 35), (37, 35), (38, 35), (38, 34), (38, 33), (38, 32)]
Run Code Online (Sandbox Code Playgroud)
这个列表描述了玩家从点 (36, 37) 移动到点 (38, 32) 应该做的动作。
我想将此列表简化为以下内容:
opti = [(36, 37), (36, 35), (38, 35), (38, 32)]
Run Code Online (Sandbox Code Playgroud)
这意味着我想将 x 固定(或 y 固定)的任何一系列步骤减少到仅第一步和最后一步。
我正在努力找出一种算法来做到这一点。我已经尝试了两个多小时,这是我目前正在尝试的工作:
solution = [(36, 37), (36, 36), (36, 35), (37, 35), (38, 35), (38, 34), (38, 33), (38, 32)]
opti = [solution[0]]
for i in range(len(solution)):
if opti[-1][0] == solution[i][0]:
pass
elif opti[-1][1] == solution[i][1]:
pass …
Run Code Online (Sandbox Code Playgroud)