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

如何修改Python的SenGen函数生成指定格式的飞机服务描述语句?

Solution to Generate Descriptive Aircraft Service Sentences

First, let’s fix the core issues with your original code: the loop logic was misaligned between aircraft types and their services, and there was no way to inject those descriptive context phrases you want. Here’s a revised approach that addresses both and produces the structured sentences you’re looking for:

Step 1: Add Context Phrases

Create a dictionary to map each aircraft type to its specific descriptive clause—this makes it easy to update or expand later:

# Define context phrases for each aircraft type
aircraft_context = {
    'Cargo Aircraft': 'that handle international and domestic freight',
    'International Aircraft': 'that operate from various airports of India',
    'Domestic Aircraft': 'which run within India'
}

Step 2: Rewrite the SenGen Function

We’ll restructure the function to properly link aircraft types to their services, inject the context phrase, and format the service list cleanly:

def SenGen(services, aircraft_types):
    # Iterate over each aircraft type in your AirCrafts dict
    for aircraft_type, _ in aircraft_types.items():
        # Fetch the corresponding services for this aircraft type
        aircraft_services = services.get(aircraft_type, [])
        
        # Handle case where no services exist
        if not aircraft_services:
            print(f"{aircraft_type} {aircraft_context.get(aircraft_type, '')} are not found.")
            continue
        
        # Print the descriptive opening sentence
        print(f"{aircraft_type} {aircraft_context.get(aircraft_type, '')} are:")
        
        # List each service as a bullet point
        for service in aircraft_services:
            print(f"• {service}")
        print()  # Add spacing between sections for readability

Step 3: Test the Function

Call it with your existing data:

AirCrafts={'Cargo Aircraft':'1','International Aircraft':'2','Domestic Aircraft':'3'}
Services={
 'Cargo Aircraft':[ "Fedx","DHFL","Emirates SkyCargo","Qatar Airways Cargo","Cathay Pacific Cargo" ],
 'International Aircraft':[ "Air India","Air France","BA" ],
 'Domestic Aircraft':[ "TruJet","Indigo","AirAsia" ]
}

SenGen(Services, AirCrafts)

Expected Output:

Cargo Aircraft that handle international and domestic freight are:
• Fedx
• DHFL
• Emirates SkyCargo
• Qatar Airways Cargo
• Cathay Pacific Cargo

International Aircraft that operate from various airports of India are:
• Air India
• Air France
• BA

Domestic Aircraft which run within India are:
• TruJet
• Indigo
• AirAsia

Key Improvements:

  • Context Injection: Uses the aircraft_context dict to seamlessly add those specific descriptive clauses you wanted.
  • Cleaner Logic: Directly maps each aircraft type to its services instead of nested loops that caused confusion.
  • Readable Formatting: Adds bullet points and spacing between sections for clarity.
  • Error Handling: Gracefully handles cases where an aircraft type has no services listed.

If you want to adjust context phrases or change service formatting (e.g., comma-separated lists instead of bullets), you can easily modify the aircraft_context dict or the service-printing loop.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.08 18:02:28