Python2.7: 保存已执行的程序步骤并逐一读取

Python2.7: Saving executed program steps and read them one by one

我遇到了以下问题。我想将一项功能集成到我的 Rasberry Pi 控制的遥控车中,使这辆遥控车回到起点。为此,它必须记录用户给汽车的每个命令,最后它应该再次执行这些命令,除非以相反的方式返回起点。

我对 stringsarrays 进行了大量研究,但其中 none 似乎是获得所需行为的好方法。

所以我的问题;有什么办法吗?

您要查找的是 stack 数据结构。它遵循 "first-in, last out" 规则。在 Python 中,通过 appendpop 可以很容易地将列表(就像数组一样)用作堆栈。这是一个代码片段,让您了解它是如何工作的:

directions = []
directions.append('forward')
#execute command here
directions.append('right')
#execute command here then turn car 180 degrees
#at this point directions = ['forward', 'right'] now you need to just pop the commands to execute
issueCommand(directions.pop()) #sends 'right'
issueCommand(directions.pop()) #sends 'forward'