You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何从嵌套结构的Python字典中提取车辆信息?

How to Extract Vehicle Information from a Nested Python Dictionary

Original Implementation Context

You previously used this code to pull car info via userid:

def car(userid): 
    return "Car: %s!" % car_models.get(userid, 'This car model does not exist in the database.') 
car_models = { 
    382: "Onix - Model 2018", 
    950: "HB20 - Model 2018", 
    590: "Citroen - Model 2016", 
}

Calling car(382) returns 'Car: Onix - Model 2018!'. Now you want to switch to using this nested dictionary instead:

cars = {'Chevrolet':{'Onix':'Model 2018', 'Selta':'Model 2017'}, 'Ford':{'Fusion':'Model 2009', 'Ranger':'Model 2015'}}

Solution Options Based on Your Needs

Option 1: Query by Brand and Model

If you plan to look up cars using their brand and model names directly, this function is straightforward:

def get_car_info(brand, model):
    # Check if the brand exists in the dictionary
    brand_data = cars.get(brand)
    if not brand_data:
        return "Car: This car brand does not exist in the database."
    
    # Check if the model exists under that brand
    model_year = brand_data.get(model)
    if not model_year:
        return "Car: This car model does not exist in the database."
    
    # Return the formatted result
    return f"Car: {model} - {model_year}!"

Example Calls:

>>> get_car_info('Chevrolet', 'Onix')
'Car: Onix - Model 2018!'
>>> get_car_info('Ford', 'Ranger')
'Car: Ranger - Model 2015!'
>>> get_car_info('Toyota', 'Corolla')
'Car: This car brand does not exist in the database.'

Option 2: Keep Using UserID-Based Lookup

If you want to retain the original userid workflow, you'll need to map user IDs to their corresponding brand-model pairs first:

# Create a mapping from userid to (brand, model)
user_car_map = {
    382: ('Chevrolet', 'Onix'),
    # Note: HB20 and Citroen from your original data aren't in the new nested dict
    # You can add their brand entries to the cars dict or adjust the message as needed
    950: ('Hyundai', 'HB20'),
    590: ('Citroen', 'C4')
}

cars = {'Chevrolet':{'Onix':'Model 2018', 'Selta':'Model 2017'}, 'Ford':{'Fusion':'Model 2009', 'Ranger':'Model 2015'}}

def car(userid):
    # Get the brand-model pair for the userid
    brand_model = user_car_map.get(userid)
    if not brand_model:
        return "Car: This userid does not exist in the database."
    
    brand, model = brand_model
    # Check brand and model in the nested dict
    brand_data = cars.get(brand)
    if not brand_data:
        return "Car: This car brand does not exist in the database."
    
    model_year = brand_data.get(model)
    if not model_year:
        return "Car: This car model does not exist in the database."
    
    return f"Car: {model} - {model_year}!"

Example Call:

>>> car(382)
'Car: Onix - Model 2018!'

Option 3: Find a Car by Model Name (Unknown Brand)

If you only know the model name and want to find its brand and year, use this traversal function:

def find_car_by_model(model):
    for brand, models in cars.items():
        if model in models:
            return f"Car: {model} - {models[model]} (Brand: {brand})!"
    return "Car: This car model does not exist in the database."

Example Call:

>>> find_car_by_model('Selta')
'Car: Selta - Model 2017 (Brand: Chevrolet)!'

内容的提问来源于stack exchange,提问作者HWarlley Massafera

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.22 07:36:54