向后枚举列表的大小,但保持列表向前移动



所以我有一些代码如下:

amount_of_episodes = len(episodes_from_user_show)
for x in episodes_from_user_show:
print("[{0}] Episode {1}: {2}".format(amount_of_episodes, x.episode_number,
x.name))  # Prints the available list of episodes.
amount_of_episodes -= 1

它所做的是它采用列表的长度,它向后迭代直到达到 0,并且我正在使用的 api 中的剧集名称和编号也被打印出来。

所以我想知道的是是否有一种更易读的方法。类似于使用枚举的东西,可以显示列表的大小向后,但列表的内容向前(如果有意义(?

一个简单的方法是打印len(values) - index

shows = [{'id': 1, 'name': 'show_1'},{'id': 2, 'name': 'show_2'}]
for idx, show in enumerate(shows):
print("[{}] Episode {}: {}".format(len(shows) - idx, show['id'], show['name']))

输出:

[2] Episode 1: show_1
[1] Episode 2: show_2

最新更新