0

I have a list of of lists like this:

nestedList = [[0],[0,1,2,3,4,6,7,8,9],[0,1,2,3,4,6,7,8,9],[1,2,3]]

and I have another homogeneous (same length) list of elements that I want to use to split the nestedList

lengthList = [[1],[5,4],[5,4],[3]]

I tried:

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))

but it doesn't work 100% right.

The output it gives is:

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]]]

instead of

[[[0], [[0, 1, 2, 3, 4], [6, 7, 8, 9]], [[0, 1, 2, 3], [4, 6, 7, 8,9]], [[1, 2, 3]]]

Any help would be appreciated.

3
  • 1
    what is the exact problem with the output? Commented Feb 5, 2016 at 19:45
  • 1
    1. describe the problem (what you are trying to achieve) please 2. if you want code corrected / improved, there's CodeReview on stackexchange for that. Commented Feb 5, 2016 at 19:50
  • 1
    what do you mean by split the list with the homogenous list? Commented Feb 5, 2016 at 19:51

3 Answers 3

2
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

Output:

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]]]
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you @inspectorG4dget!
0

try this code

# 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))

Comments

0

Here is the solution you need

# 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))

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.