使用 python 列表和字典分批消耗值

batchwise values consumption using python list and dictionary

大家好,我需要一些帮助来使用 python 寻找数据结构问题的解决方案。 我有两个数据结构,包含特定批次的批次数量的批次字典和第二个数据结构 so_data 是订单列表。现在的任务是循环 so_data 并在 FIFO 的基础上分批消耗批量数量。

batches = {'batch1': 200, 'batch2': 300, 'batch3': 200, 'batch4': 200,
         'batch5': 400, 'batch6': 100, 'batch7': 200, 'batch8': 300}

so_data = [50, 50, 50, 10, 340, 10, 40, 20, 150, 330, 50, 150, 20, 50, 30, 100, 60]


def batchwise_consumption(so_data, batches):
    print(batches)
    for so_qty in so_data:

        for bch,bch_qty in batches.items():
            if bch_qty > 0:
                print('order qty: ', so_qty, ', curr batch qty: ', bch_qty, ', curr batch: ', bch)
                if bch_qty >= so_qty:
                    # order fullfilled by one batch
                    batches[bch] = bch_qty - so_qty
                    print('\trem qty: ', batches[bch])
                    break # break on full filling order
                else:
                    # order fullfilled by partial batches
                    partial_qty = batches[bch]
                    batches[bch] = batches[bch] - batches[bch]
                    rem_so_qty = so_qty-partial_qty
                    print('\tpartial qty', partial_qty, ', rem so qty', rem_so_qty)
                    while partial_qty != so_qty:
                        # make so_qty from multiple batches 
                        break # break on full filling order

    print(batches)
batchwise_consumption(so_data, batches)

这个问题是数据集的一部分,下面是分批消费的模拟

感谢任何帮助或更好的解决方案。 在此先感谢您的帮助。

终于解决了: 好吧,任务是为销售订单列表消耗批量库存。所以在循环销售订单后我检查了:

如果 so_qty 小于批次数量并且符合批次数量的给定索引,则从批次数量减少 so_qty,然后将当前批次附加到字典的已消耗批次列表中,该字典维护每个订单的批次消耗,最后将当前 so_qty 设置为 0,因为订单已完成

否则,如果 so_qty 大于从当前 so_qty 减少的批次数量,将剩余的批次数量添加到已消耗的批次列表,将当前批次设置为 0 并继续相同的 so_qty,除非它变成0 并在 0.

时断开当前 so_qty 的循环
batches = {'batch1': 200, 'batch2': 300, 'batch3': 200, 'batch4': 200,
         'batch5': 400, 'batch6': 100, 'batch7': 200, 'batch8': 300, 'batch9': 50}

so_data = [50, 50, 50, 10, 340, 10, 40, 20, 150, 330, 50, 150, 20, 50, 30, 100, 60, 400]

def batchwise_consumption(so_data, batches):
    order_no=0
    for so_qty in so_data:
        order_no +=1
        print(order_no, '\tso qty ', so_qty)
        baches_consumed = []
        for bch,bch_qty in batches.items():
            if bch_qty > 0:
                if so_qty <= bch_qty:
                    batches[bch] = bch_qty - so_qty
                    baches_consumed.append({bch: so_qty})
                    so_qty = 0

                elif so_qty > bch_qty:
                    so_qty = so_qty - bch_qty
                    batches[bch] = 0
                    baches_consumed.append({bch: bch_qty})
                    continue

                if so_qty == 0:
                    break
        print('\t', baches_consumed)

batchwise_consumption(so_data, batches)