如何将python列表拆分为不同长度的列表? [英] How to split python list into lists of different lengths?
本文介绍了如何将python列表拆分为不同长度的列表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何制作可以执行列表推导的函数(zip?)子列表应该有不同的长度.
How to make function that can execute list comprehension(zip?) The sublists should have different lengths which are given.
示例:
myList = [1,2,3,4,5,6,7,8,9]
myNum = (2,4,3)
预期结果:
newList = ['(1,2)', '(3,4,5,6)', '(7,8,9)']
推荐答案
如果不是必须是List comprehension,可以这样解决:
If it does not have to be a List comprehension, you can solve it like this:
myList = [1,2,3,4,5,6,7,8,9]
myNum = (2,4,3)
prev = 0
newList = []
for i in myNum:
newList.append(tuple(myList[prev:prev+i]))
prev = prev+i
这篇关于如何将python列表拆分为不同长度的列表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文