Search code examples
arrayspython-3.xnested-lists

Split the array in a list


I have a list which has an array like

ListA = [('8', array([[422, 769, 468, 789],
         [422, 739, 468, 759],
         [422, 709, 468, 729],
         [422, 545, 468, 565],
         [422, 515, 468, 535]]), 'A'),
         ('8', array([[423, 483, 466, 506]]), 'A'),
         ('8', [], 'B'),
         ('9', array([[375, 579, 414, 619],
         [375, 549, 414, 589],
         [375, 519, 414, 559]]), 'C')]

Now the array in the list is to be splitted in such a way that output looks like

ListA = [('8', [422, 769, 468, 789], 'A'),
         ('8', [422, 739, 468, 759], 'A'),
         ('8', [422, 709, 468, 729], 'A'),
         ('8', [422, 545, 468, 565], 'A'),
         ('8', [422, 515, 468, 535], 'A'),
         ('8', [423, 483, 466, 506], 'A'),
         ('8', [], 'B'),
         ('9', [375, 579, 414, 619], 'C'),
         ('9', [375, 549, 414, 589], 'C'),
         ('9', [375, 519, 414, 559], 'C')]

Is there any way to to do this ?


Solution

  • output = []
    for line in ListA:
        number, arr, char = line
        if len(arr) == 0:  #append empty row
            output.append((number, [], char))
    
        for row in arr:
            output.append((number, row, char))