如何将嵌套列表转换为字典对象并修正字段错位问题?
Solution: Convert Nested List to Formatted Dictionary with Swap Logic
Absolutely, we can convert your nested list into the desired dictionary format—including swapping the id and email values when the id position contains an email address. Here's a practical, Python-based approach to get this done:
Step-by-Step Breakdown
- Extract inner values: Each entry in your input is a nested sublist (e.g.,
[[['1', '1@1']]]), so we first pull out the pair of values we care about. - Identify email vs. ID: We check which value contains an
@symbol (the standard marker for an email). If the first value is an email and the second isn't, we swap them. - Normalize ID type: We convert numeric ID strings to integers or floats (matching your examples like
8.5), and leave non-numeric IDs as strings if needed. - Build the result: For each processed pair, we create a dictionary with
idandemailkeys and add it to our final list.
Complete Code
# Your input nested list input_list = [[['1', '1@1']], [['2', '2@2.com']], [['3', '3@3.com']], [['4', '4@4.com']], [['5', '5@5.com']], [['6', '6@6']], [['7', '7@7']], [['8', '8@8']], [['8.5', '8.5@8.5']], [['9', '9@9']], [['10', '10@10']], [['11', '11@11']], [['12', '12@12']], [['13', '13@13.com']], [['14', '14@14.com']], [['15', '15@15.com']], [['16', '16@16.com']], [['17', '17@17.com']], [['18@18.com', '18']], [['19', '19@19.com']]] result = [] def is_numeric(s): """Helper to check if a string can be converted to a number""" try: float(s) return True except ValueError: return False for entry in input_list: # Get the inner pair of values first_val, second_val = entry[0] # Check which value is the email first_is_email = '@' in first_val second_is_email = '@' in second_val # Handle swapping logic if first_is_email and not second_is_email: email = first_val id_str = second_val elif not first_is_email and second_is_email: email = second_val id_str = first_val else: # Edge case: both/neither have @—use numeric check to decide if is_numeric(first_val) and not is_numeric(second_val): id_str = first_val email = second_val elif is_numeric(second_val) and not is_numeric(first_val): id_str = second_val email = first_val else: # Fallback: assume first is ID, second is email id_str = first_val email = second_val # Convert ID to appropriate numeric type (int/float) try: id_val = int(id_str) except ValueError: try: id_val = float(id_str) except ValueError: id_val = id_str # Keep as string if non-numeric # Add the formatted dictionary to the result result.append({'id': id_val, 'email': email}) # Print the final output print(result)
Sample Output
The code will produce a list of dictionaries matching your desired format. Here's a truncated preview:
[ {'id': 1, 'email': '1@1'}, {'id': 2, 'email': '2@2.com'}, ..., {'id': 18, 'email': '18@18.com'}, # Correctly swapped! {'id': 19, 'email': '19@19.com'} ]
This solution handles all edge cases in your input—including the swapped entry at position 18—and ensures numeric IDs are converted to their proper integer/float types.
内容的提问来源于stack exchange,提问作者RustyShackleford
相关产品推荐
相关产品推荐

