Python:根据索引范围将列表拆分为子列表 [英] Python: Split a list into sub-lists based on index ranges
本文介绍了Python:根据索引范围将列表拆分为子列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
更新:
在python中,如何根据索引范围将列表拆分为子列表
In python, how do i split a list into sub-lists based on index ranges
例如原始清单:
list1 = [x,y,z,a,b,c,d,e,f,g]
使用索引范围0 - 4:
using index ranges 0 - 4:
list1a = [x,y,z,a,b]
使用索引范围5-9:
list1b = [c,d,e,f,g]
谢谢!
我已经知道(变量)指数列表元素包含某些字符串并希望根据这些索引值拆分列表。
I already known the (variable) indices of list elements which contain certain string and want to split the list based on these index values.
还需要拆分成可变数量的子列表!即:
Also need to split into variable number of sub-lists! i.e:
list1a
list1b
.
.
list1[x]
推荐答案
请注意可以在切片中使用变量:
Note that you can use a variable in a slice:
l = ['a',' b',' c',' d',' e']
c_index = l.index("c")
l2 = l[:c_index]
这会将l的前两个条目放在l2
This would put the first two entries of l in l2
这篇关于Python:根据索引范围将列表拆分为子列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文