Can you take a list of lists of unequal length and write it in a row of columns when matching with the order of another list?
order = ['bx', 'cs', 'lb', 'pc']
totals = [
[{'unit': 'pc', 'sum': Decimal('3.00')}],
[{'unit': 'bx', 'sum': Decimal('3.00')}, {'unit': 'pc', 'sum': Decimal('16.00')}],
[{'unit': 'bx', 'sum': Decimal('6.00')}, {'unit': 'lb', 'sum': Decimal('24.00')}, {'unit': 'pc', 'sum': Decimal('63.00')}],
[{'unit': 'pc', 'sum': Decimal('36.00')}],
[{'unit': 'bx', 'sum': Decimal('31.00')}]
]
desired_format = [
['', '', '', '3.00 pc'],
['3.00 bx', '', '', '16.00 pc'],
['6.00 bx', '', '24.00 lb', '63:00 pc'],
['', '', '', '36:00 pc'],
['31.00 bx', '', '', ''],
]
I tried below, but it does not work as desired if all 4 units are in order missing.
desired_format = [[]]
for total in totals:
data = []
for idx, um in enumerate(total):
if um['unit'] == order[idx]:
data.append(str(um['sum'] + ' ' + str(um['unit'])))
else:
data.append('')
desired_format.append(data)
I also tried this, which ends up with too many empty columns and uneven / unordered columns
desired_format = [[]]
for total in totals:
data = []
for um in total:
for unit in order:
if um['unit'] == unit:
data.append(str(um['sum'] + ' ' + str(um['unit'])))
else:
data.append('')
desired_format.append(data)