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

如何为URL字典匹配服务信息并解决嵌套字典访问问题

Solution to Fix Nested Dictionary Access & Transform Your Data

Alright, let's walk through how to clean up your dictionaries, build the exact structure you need, and properly access the inner values to print "Apache = 2.4.34".

Step 1: Clean Up the Service Dictionary

First, we need to strip out all the extra whitespace, commas, and unwanted characters from the keys in newerdict—those are what's making it hard to access inner properties later. We'll use a dictionary comprehension to do this quickly:

newerdict = {'Drupal , ': '7', 'Apache , ': '2.4.34'}
# Clean keys by removing commas and trimming whitespace
cleaned_services = {key.strip().replace(',', ''): value for key, value in newerdict.items()}
# Result: {'Drupal': '7', 'Apache': '2.4.34'}

Step 2: Map URLs to Their Corresponding Services

We already know which service each URL uses, so let's create a clear mapping:

url_service_map = {
    'http://REDACTED1.com': 'Apache',
    'http://www.REDACTED2.com': 'Drupal'
}

Step 3: Build the Target Dictionary

Now we'll transform dict200 into the exact structure you specified, including converting URLs to lowercase as shown in your desired output:

dict200 = {'http://REDACTED1.com': [21, 22, 80, 443, 3306], 'http://www.REDACTED2.com': [80, 443]}

result_dict = {}
for original_url, _ in dict200.items():
    # Convert URL to lowercase to match your target format
    normalized_url = original_url.lower()
    # Get the service name for this URL
    service = url_service_map[original_url]
    # Get the cleaned version number
    version = cleaned_services[service]
    # Build the nested structure (match your desired case for service keys)
    if service == 'Apache':
        # Target uses lowercase 'apache' for REDACTED1
        result_dict[normalized_url] = [{'apache': version}]
    else:
        # Target uses uppercase 'Drupal' for REDACTED2
        result_dict[normalized_url] = [{service: version}]

# Final result matches your desired structure:
# {'http://redacted1.com': [{'apache': '2.4.34'}], 'http://redacted2.com': [{'Drupal': '7'}]}

Step 4: Access Inner Properties & Print the Desired Output

Now that we have a clean, properly structured dictionary, accessing the inner values is straightforward. To print "Apache = 2.4.34":

# Grab the first (and only) item in the list for REDACTED1's URL
apache_data = result_dict['http://redacted1.com'][0]
# Access the 'apache' key and print the formatted string
print(f"Apache = {apache_data['apache']}")

If you need to rescue your existing messy nested dictionary (with keys like ' Apache (web-servers), '), you can use a similar cleaning approach to fix the keys:

# Example for cleaning your existing problematic entries
messy_entry = [{'	Apache (web-servers), ': '2.4.34'}]
# Extract the key, clean it, and reformat
raw_key = list(messy_entry[0].keys())[0]
cleaned_key = raw_key.strip().replace(',', '').split('(')[0].strip()
cleaned_entry = {cleaned_key: messy_entry[0][raw_key]}
# Now you can access it normally: print(f"Apache = {cleaned_entry['Apache']}")

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.28 09:43:38