我正在使用 Python Crash Course book 学习 python,在使用用户输入填充列表时进行练习。我在下面完成了这个练习,但想学习如何更改代码以使列表的顺序匹配。
我阅读了 Python 列表作为 FIFO,使用双端队列的 LIFO 队列,但还不了解如何使用这些数据结构。
sandwich_orders = ['cheese', 'ham', 'turkey', 'pb&j', 'chicken salad']
finished_sandwiches = []
for sandwich in sandwich_orders:
print("I received your " + sandwich + " sandwich order.")
while sandwich_orders:
current_sandwich = sandwich_orders.pop()
print("Making " + current_sandwich.title() + " sandwich.")
finished_sandwiches.append(current_sandwich)
print("\nThe following sandwiches have been made:")
for sandwich in finished_sandwiches:
print(sandwich.title())
打印 current_sandwich 列表的顺序与 Sandwich_orders 列表相反。我希望 current_sandwich 以与 Sandwich_orders 列表相同的顺序打印。