[英]How to get list indices after a specific index
If I have the list: ['a', 'b', 'c', 'd', 'e']
, and I want a variable like this: afterC = ['d','e']
- Aka, all items after 'c' get put into their own list.如果我有列表:
['a', 'b', 'c', 'd', 'e']
,我想要一个像这样的变量: afterC = ['d','e']
- Aka, 'c' 之后的所有项目都放入自己的列表中。 I've tried searching for a solution, but I can't find anything我已经尝试寻找解决方案,但我找不到任何东西
The list
object has an .index()
method which returns the index of a given value. list
对象有一个.index()
方法,它返回给定值的索引。 This index can be used to slice the list and return the values after the given index.该索引可用于对列表进行切片并返回给定索引之后的值。
For example:例如:
l = ['a', 'b', 'c', 'd', 'e']
l2 = l[l.index('c') + 1:]
Output:输出:
>>> l2
['d', 'e']
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.