我有这样一个列表:
nestedList = [[0],[0,1,2,3,4,6,7,8,9],[0,1,2,3,4,6,7,8,9],[1,2,3]]
我还有另一个同质(相同长度)的元素列表,我想用它来分割nestedList
lengthList = [[1],[5,4],[5,4],[3]]
我试过:
def split(arr, size):
arrs = []
while len(arr) > size:
pice = arr[:size]
arrs.append(pice)
arr = arr[size:]
arrs.append(arr)
return arrs
for i,j in zip(nestedList,lengthList):
for k in j:
myNewList.append(split(i,k))
但这并不是100%正确。你知道吗
它的输出是:
myNewList = [[[0]], [[0, 1, 2, 3, 4], [6, 7, 8, 9]], [[0, 1, 2, 3], [4, 6, 7, 8], [9]], [[0, 1, 2, 3, 4], [6, 7, 8, 9]], [[0, 1, 2, 3], [4, 6, 7, 8], [9]], [[1, 2, 3]]]
而不是
[[[0], [[0, 1, 2, 3, 4], [6, 7, 8, 9]], [[0, 1, 2, 3], [4, 6, 7, 8,9]], [[1, 2, 3]]]
任何帮助都将不胜感激。你知道吗
这是你需要的解决方案
输出:
尝试此代码
相关问题 更多 >
编程相关推荐