Python 如何使用列表切片从列表中获取除第一个元素之外的所有内容

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/40443331/
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 23:33:10  来源:igfitidea点击:

How to get everything from the list except the first element using list slicing

pythonpython-2.7listpython-3.x

提问by Omid CompSCI

So I have something that I am parsing, however here is an example of what I would like to do:

所以我有一些我正在解析的东西,但是这里是我想做的一个例子:

list = ['A', 'B', 'C']

And using list slicing have it return to me everything but the first index. So in this case:

并使用列表切片让它返回给我除了第一个索引之外的所有内容。所以在这种情况下:

['B', 'C']

I have been messing with stuff like list[:-1], list[::-1], list[0:-1], etc. But I can't seem to be able to find this out.

我一直在搞乱 list[:-1]、list[::-1]、list[0:-1] 等东西。但我似乎无法找到它。

What I am actual doing is: * I have a error message that has a error code in the beginning such as:

我实际做的是: * 我有一条错误消息,开头有一个错误代码,例如:

['226', 'Transfer', 'Complete']

and I want to just display Transfer Complete on a popup widget. Of course I am casting to a string.

我只想在弹出窗口小部件上显示传输完成。当然,我正在转换为字符串。

Thank you for all help, and if answer differs via Python 2.7.x and Python 3.x.x Please answer for both versions.

感谢您的所有帮助,如果 Python 2.7.x 和 Python 3.xx 的答案不同,请回答这两个版本。

Thanks, looked a lot around stackoverflow and python tutorials couldn't really quite get what I was looking for. Thanks for your help!

谢谢,看了很多关于 stackoverflow 和 python 教程不能真正得到我想要的东西。谢谢你的帮助!

回答by

You can just do [1:]. This will work on both versions.

你可以只做 [1:]。这将适用于两个版本。

回答by Люда Лапицкая

It can be done like this:

可以这样做:

list[1:]