以下是一个示例代码,可以将订单分批排序为4个批次:
# 订单列表
orders = [
{'order_id': 'A001', 'product': 'Product A'},
{'order_id': 'A002', 'product': 'Product B'},
{'order_id': 'A003', 'product': 'Product A'},
{'order_id': 'A004', 'product': 'Product C'},
{'order_id': 'A005', 'product': 'Product B'},
{'order_id': 'A006', 'product': 'Product A'},
{'order_id': 'A007', 'product': 'Product C'},
{'order_id': 'A008', 'product': 'Product A'},
{'order_id': 'A009', 'product': 'Product B'},
{'order_id': 'A010', 'product': 'Product C'}
]
# 按产品进行排序
orders.sort(key=lambda x: x['product'])
# 每个批次的订单数量
batch_size = len(orders) // 4
# 分批排序
batches = [orders[i:i + batch_size] for i in range(0, len(orders), batch_size)]
# 打印结果
for i, batch in enumerate(batches):
print(f'Batch {i + 1}:')
for order in batch:
print(order)
print()
这个示例代码首先将订单列表按照产品进行排序,然后根据订单数量计算出每个批次的大小。接下来,使用列表切片将订单列表分成4个批次,并将每个批次的订单打印出来。