如何在python中将元素n中的列表切成末尾? [英] How to slice a list from an element n to the end in python?

查看:130
本文介绍了如何在python中将元素n中的列表切成末尾?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在弄清楚如何分割python列表时遇到了一些麻烦,其示例如下:

I'm having some trouble figuring out how to slice python lists, it is illustrated as follows:

>>> test = range(10)
>>> test
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> test[3:-1]
[3, 4, 5, 6, 7, 8]
>>> test[3:0]
[]
>>> test[3:1]
[]
>>> test
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

据我了解,python slice的意思是lst [start:end],包括start,不包括end.那么,我将如何查找从元素n开始的列表的其余部分"?

To my understanding, python slice means lst[start:end], and including start, excluding end. So how would i go about finding the "rest" of a list starting from an element n?

非常感谢您的帮助!

推荐答案

您可以通过不指定该值来使片的一端保持打开状态.

You can leave one end of the slice open by not specifying the value.

test[3:] = [3, 4, 5, 6, 7, 8, 9]
test[:3] = [0, 1, 2]

这篇关于如何在python中将元素n中的列表切成末尾?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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