使用负索引
my_list = ['a', 'b', 'c', 'd', 'e']
# print the last element
print(my_list[-1])
输出
e
当您使用负索引时,计数从 1 开始,而不是 0,如下图所示。

如果您想要第一个元素,可以使用 `my_list[-5]`。
另请阅读
成为一名认证的 Python
程序员。
成为一名认证的 Python
程序员。
要理解这个例子,你应该具备以下 Python 编程 主题的知识
my_list = ['a', 'b', 'c', 'd', 'e']
# print the last element
print(my_list[-1])
输出
e
当您使用负索引时,计数从 1 开始,而不是 0,如下图所示。
如果您想要第一个元素,可以使用 `my_list[-5]`。
另请阅读