Python 使用DataFrame构建带有while循环的列表

Python 使用DataFrame构建带有while循环的列表,python,pandas,dataframe,Python,Pandas,Dataframe,假设我们是Amazon,我们收到了一份客户订单,订单的格式如下所示不知道为什么要使用pandas-您可以使用简单的python,而不需要任何导入: # ok, 1 import - but that is just for printing convenience ;) from pprint import pprint od = {"order_id": 123, "requested": [{"product_id": 0, &q

假设我们是Amazon,我们收到了一份客户订单,订单的格式如下所示不知道为什么要使用pandas-您可以使用简单的python,而不需要任何导入:

# ok, 1 import - but that is just for printing convenience ;)
from pprint import pprint

od = {"order_id": 123, "requested": [{"product_id": 0, "quantity": 2}, 
                                     {"product_id": 10, "quantity": 4}]} 

# badly structured - we reformat it into a lookup dict based on product_id
pi = [{"mass_g": 700, "product_name": "RBC A+ Adult", "product_id": 0}, 
    {"mass_g": 700, "product_name": "RBC B+ Adult", "product_id": 1}, 
    {"mass_g": 750, "product_name": "RBC AB+ Adult", "product_id": 2}, 
    {"mass_g": 680, "product_name": "RBC O- Adult", "product_id": 3}, 
    {"mass_g": 350, "product_name": "RBC A+ Child", "product_id": 4}, 
    {"mass_g": 200, "product_name": "RBC AB+ Child", "product_id": 5}, 
    {"mass_g": 120, "product_name": "PLT AB+", "product_id": 6}, 
    {"mass_g": 80, "product_name": "PLT O+", "product_id": 7}, 
    {"mass_g": 40, "product_name": "CRYO A+", "product_id": 8}, 
    {"mass_g": 80, "product_name": "CRYO AB+", "product_id": 9}, 
    {"mass_g": 300, "product_name": "FFP A+", "product_id": 10}, 
    {"mass_g": 300, "product_name": "FFP B+", "product_id": 11}, 
    {"mass_g": 300, "product_name": "FFP AB+", "product_id": 12}]

# better usable lookup
bpi = {dic["product_id"]:dic for dic in pi }

# get a list of all products for scheduling for packing
products = [ bpi[k] for b in ([inner["product_id"]]*inner["quantity"] 
                               for inner in od["requested"]) for k in b]

pprint(products)
print()

# packaging
max_w = 1800
# we start with one empty package and remember how much room is left
remaining = { 0 : max_w}
packages = [[]]

# now we schedule stuff, if needed we create a new empty package with appropriate room
# if one item does not fit any already prepared packages
for p in products:
    mass = p["mass_g"]
    done = False
    # look if it fits anywhere
    for i in remaining:
        if remaining[i] >= mass:
            packages[i].append(p)
            remaining[i] -= mass
            done = True
            break
    # does not fit, use new package
    if not done:
        remaining[i+1] = max_w - mass
        packages.append([p])

pprint(packages)
输出:

# order items
[{'mass_g': 700, 'product_id': 0, 'product_name': 'RBC A+ Adult'},
 {'mass_g': 700, 'product_id': 0, 'product_name': 'RBC A+ Adult'},
 {'mass_g': 300, 'product_id': 10, 'product_name': 'FFP A+'},
 {'mass_g': 300, 'product_id': 10, 'product_name': 'FFP A+'},
 {'mass_g': 300, 'product_id': 10, 'product_name': 'FFP A+'},
 {'mass_g': 300, 'product_id': 10, 'product_name': 'FFP A+'}]

# packaging
[[{'mass_g': 700, 'product_id': 0, 'product_name': 'RBC A+ Adult'},
  {'mass_g': 700, 'product_id': 0, 'product_name': 'RBC A+ Adult'},
  {'mass_g': 300, 'product_id': 10, 'product_name': 'FFP A+'}],
 [{'mass_g': 300, 'product_id': 10, 'product_name': 'FFP A+'},
  {'mass_g': 300, 'product_id': 10, 'product_name': 'FFP A+'},
  {'mass_g': 300, 'product_id': 10, 'product_name': 'FFP A+'}]]

如果您想要更短的输出,可以重新组合数据。

非常感谢!!!这让我难堪了很长很长时间!你救了我。