在列表中获取倒数第二个元素 [英] Getting Second-to-Last Element in List

查看:999
本文介绍了在列表中获取倒数第二个元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以使用以下内容获取列表的倒数第二个元素:

I am able to get the second-to-last element of a list with the following:

>>> lst = ['a', 'b', 'c', 'd', 'e', 'f']
>>> print(lst[len(lst)-2])
e

是否有比使用print(lst[len(lst)-2])更好的方法来达到相同的结果?

Is there a better way than using print(lst[len(lst)-2]) to achieve this same result?

推荐答案

有:负索引:

lst[-2]

这篇关于在列表中获取倒数第二个元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆