Python 获取列表中的倒数第二个元素
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/39838900/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 22:47:32 来源:igfitidea点击:
Getting Second-to-Last Element in List
提问by PyNoob
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
Is there a better way than using print(lst[len(lst)-2])
to achieve this same result?
有没有比使用更好的方法print(lst[len(lst)-2])
来实现相同的结果?
回答by Scott Hunter
There is: negative indices:
有: 负指数:
lst[-2]