Home > OS >  List of lists into a list of lists of a specific length with condition
List of lists into a list of lists of a specific length with condition

Time:03-24

data = [
    
    ['customers/2309565764/assets/34830517871', 
    'customers/2309565764/assets/34827154141', 
    'customers/2309565764/assets/34856605170', 
    'customers/2309565764/assets/34830515300'
    ],
     
    ['customers/2309565764/assets/34830515303', 
    'customers/2309565764/assets/34830473309', 
    'customers/2309565764/assets/34830508136', 
    'customers/2309565764/assets/34830515336'], 

    ['customers/2309565764/assets/20029085042', 
    'customers/2309565764/assets/20033811553', 
    'customers/2309565764/assets/20065471524', 
    'customers/2309565764/assets/20029089104'], 

    ['customers/2309565764/assets/20033813263', 
    'customers/2309565764/assets/20065476771', 
    'customers/2309565764/assets/20029091753', 
    'customers/2309565764/assets/20029091369'],

    ['customers/2309565764/assets/11111', 
    'customers/2309565764/assets/22222', 
    'customers/2309565764/assets/33333', 
    ],

    ['customers/2309565764/assets/20033813263', 
    'customers/2309565764/assets/20065476771', 
    'customers/2309565764/assets/20029091753', 
    'customers/2309565764/assets/20029091369'],

    ['customers/2309565764/assets/20033813263', 
    'customers/2309565764/assets/20065476771', 
    'customers/2309565764/assets/20029091753', 
    'customers/2309565764/assets/20029091369'],

    ['customers/2309565764/assets/20033813263', 
    'customers/2309565764/assets/20065476771', 
    'customers/2309565764/assets/20029091753', 
    'customers/2309565764/assets/20029091369'],

    ['customers/2309565764/assets/20033813263', 
    'customers/2309565764/assets/20065476771', 
    'customers/2309565764/assets/20029091753', 
    'customers/2309565764/assets/20029091369'],

    ['customers/2309565764/assets/20033813263', 
    'customers/2309565764/assets/20065476771', 
    'customers/2309565764/assets/20029091753', 
    'customers/2309565764/assets/20029091369'],

    ['customers/2309565764/assets/20033813263', 
    'customers/2309565764/assets/20065476771', 
    'customers/2309565764/assets/20029091753', 
    'customers/2309565764/assets/20029091369'],
    ]

what I want to get out of it

The same list of lists, but each of the internal lists should have no more than 20 items. It will end up looking like this

[
    ['customers/2309565764/assets/34830517871', 
    'customers/2309565764/assets/34827154141', 
    'customers/2309565764/assets/34856605170', 
    ....
    20th element],
    
    ['customers/2309565764/assets/34830517871', 
    'customers/2309565764/assets/34827154141', 
    'customers/2309565764/assets/34856605170', 
    ....
    20th element],
    
    ['customers/2309565764/assets/34830517871', 
    'customers/2309565764/assets/34827154141', 
    'customers/2309565764/assets/34856605170', 
    ....
    last element],
    
    ]

Using this code

result = []
transitional_data = []
for i in data:
    if len(transitional_data)   len(i) <= 20:
        for j in i:
            transitional_data.append(j)
    else:
        result.append(transitional_data)
        transitional_data = []
print(result)    

I end up with only one nested list instead of 3 (two of which will have 20 elements and one with 4 residual elements)

[
    ['customers/2309565764/assets/34830517871', 
    'customers/2309565764/assets/34827154141', 
    'customers/2309565764/assets/34856605170', 
    ....
    20th element],  
] 

Important condition on nested lists in data - the elements of a nested list cannot be separated, i.e. for example the first two elements of

['customers/2309565764/assets/34830517871', 
'customers/2309565764/assets/34827154141', 
'customers/2309565764/assets/34856605170', 
'customers/2309565764/assets/34830515300'
],

can't get into the first sublist, and the other two can't get into the other one. There can be <= 4 items in the original nested lists.

upd I updated input data, now 5th nested list consist 3 elements. And if I run

flat_list = [item for sublist in data for item in sublist]
partitioned_list = [flat_list[i:i   20] for i in range(0, len(flat_list), 20)] 

Result data consist 3 nested list - this is what I need to get. But first element of 6th nested list is in the first resulted sub-list and the other 2 are in the second result sub-list

CodePudding user response:

Here's a version that takes the special condition into consideration. I simplified the values in the data list to make it easier to read the input and output.

data = [
    ['1', '2', '3', '4'],
    ['5', '6', '7', '8'],
    ['9', '10', '11', '12'],
    ['13', '14', '15', '16'],
    ['17', '18', '19'],

    ['20', '21', '22', '23'],
    ['24', '25', '26', '27'],
    ['28', '29', '30', '31'],
    ['32', '33', '34', '35'],
    ['36', '37', '38', '39'],

    ['40', '41', '42', '43']
]


def combine(original_list, maxlen=20):
    outer_list = []
    accum = []

    for sublist in original_list:
        # if the next set of values will fit, extend the sublist
        if len(accum)   len(sublist) <= maxlen:
            accum.extend(sublist)
        else:
            # otherwise start a new sublist
            outer_list.append(accum)
            accum = list(sublist)

    # pick up any values that are left over
    if accum:
        outer_list.append(accum)

    return outer_list

new_list = combine(data)
print(new_list)

Based on the idea in Stef's comment, this accumulates values in a list until the maximum length is reached, but without splitting the sublists in the original input.

Output:

[['1', '2', '3', '4', '5', '6', '7', '8', '9', '10', '11', '12', '13', '14', '15', '16', '17', '18', '19'],
['20', '21', '22', '23', '24', '25', '26', '27', '28', '29', '30', '31', '32', '33', '34', '35', '36', '37', '38', '39'],
['40', '41', '42', '43']]
  • Related