Python 按循环中的值将列表拆分为嵌套列表(迭代值)
我有这样一个列表:Python 按循环中的值将列表拆分为嵌套列表(迭代值),python,list,split,nested,iteration,Python,List,Split,Nested,Iteration,我有这样一个列表: 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
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]]]
任何帮助都将不胜感激
nestedList = [[0],[0,1,2,3,4,6,7,8,9],[0,1,2,3,4,6,7,8,9],[1,2,3]]
lengthList = [[1],[5,4],[5,4],[3]]
answer = []
for lens,sub in zip(lengthList, nestedList):
answer.append([])
top = 0
for l in lens:
answer[-1].append(sub[top:top+l])
top += l
输出:
In [2]: answer
Out[2]:
[[[0]],
[[0, 1, 2, 3, 4], [6, 7, 8, 9]],
[[0, 1, 2, 3, 4], [6, 7, 8, 9]],
[[1, 2, 3]]]
输出:
In [2]: answer
Out[2]:
[[[0]],
[[0, 1, 2, 3, 4], [6, 7, 8, 9]],
[[0, 1, 2, 3, 4], [6, 7, 8, 9]],
[[1, 2, 3]]]
这是您需要的解决方案
# stackoverflow.com
# Python
# split list into nested lists by values in a loop (iterating values)
nestedList = [[0],[0,1,2,3,4,6,7,8,9],[0,1,2,3,4,6,7,8,9],[1,2,3]]
lengthList = [[1],[5,4],[5,4],[3]]
def toOneList (array):
oneList = []
if len(array) > 0:
for subList in array:
for x in subList:
oneList.append(x)
return oneList
def grouping(array, size):
newList = []
i = 0
for j in size:
newList.append(array[i:i+j])
i = i + j
return newList
nestedList = toOneList(nestedList)
lengthList = toOneList(lengthList)
print(grouping(nestedList, lengthList))
这是您需要的解决方案
# stackoverflow.com
# Python
# split list into nested lists by values in a loop (iterating values)
nestedList = [[0],[0,1,2,3,4,6,7,8,9],[0,1,2,3,4,6,7,8,9],[1,2,3]]
lengthList = [[1],[5,4],[5,4],[3]]
def toOneList (array):
oneList = []
if len(array) > 0:
for subList in array:
for x in subList:
oneList.append(x)
return oneList
def grouping(array, size):
newList = []
i = 0
for j in size:
newList.append(array[i:i+j])
i = i + j
return newList
nestedList = toOneList(nestedList)
lengthList = toOneList(lengthList)
print(grouping(nestedList, lengthList))
试试这个代码
# stackoverflow.com
# Python
# split list into nested lists by values in a loop (iterating values)
nestedList = [[0],[0,1,2,3,4,6,7,8,9],[0,1,2,3,4,6,7,8,9],[1,2,3]]
lengthList = [[1],[5,4],[5,4],[3]]
def toOneList (array):
oneList = []
if len(array) > 0:
for subList in array:
for x in subList:
oneList.append(x)
return oneList
def grouping(array, size):
newList = []
i = 0
for j in size:
newList.append(array[i:i+j])
i = i + j
return newList
nestedList = toOneList(nestedList)
lengthList = toOneList(lengthList)
print(grouping(nestedList, lengthList))
试试这个代码
# stackoverflow.com
# Python
# split list into nested lists by values in a loop (iterating values)
nestedList = [[0],[0,1,2,3,4,6,7,8,9],[0,1,2,3,4,6,7,8,9],[1,2,3]]
lengthList = [[1],[5,4],[5,4],[3]]
def toOneList (array):
oneList = []
if len(array) > 0:
for subList in array:
for x in subList:
oneList.append(x)
return oneList
def grouping(array, size):
newList = []
i = 0
for j in size:
newList.append(array[i:i+j])
i = i + j
return newList
nestedList = toOneList(nestedList)
lengthList = toOneList(lengthList)
print(grouping(nestedList, lengthList))
输出的确切问题是什么。请描述问题(你正在努力实现的目标)2。如果您希望更正/改进代码,stackexchange上会有CodeReview。您所说的
将列表拆分为同质列表是什么意思?输出的确切问题是什么?1。请描述问题(你正在努力实现的目标)2。如果您想更正/改进代码,stackexchange上会有CodeReview。您所说的将列表拆分为同质列表是什么意思?谢谢@inspectorG4dget!谢谢@inspectorG4dget!