Python新手求助:提取姓名首字母并与字典数据对比
Hey there! Let's walk through solving this problem step by step—since you're new to Python, I'll break everything down clearly so you can follow along.
First, let's recap what you need to do: take a user's first and last name, extract relevant initials, then check those initials against your pre-defined dictionary to find matching entries.
Here's a complete, working version of the code with explanations:
# Your pre-loaded dictionary (from the external file) dict1 = {'EV': ' Erik Vils', 'EVI': ' Erik Vils', 'EVIL': ' Erik Vils', 'RH': ' Rasmus Holst', 'RHO': ' Rasmus Holst', 'KA': ' Kasper Andersen'} # Get user input for first and last name firstname = input('Write first name: ') surname = input('Write surname: ') # Step 1: Clean and standardize input # Convert to uppercase (to match dictionary keys) and remove extra spaces firstname_upper = firstname.strip().upper() surname_upper = surname.strip().upper() # Step 2: Generate possible initial combinations to check # We'll create a list of initial strings that might exist in your dictionary initials_to_check = [] # 1. First initial of first name + first initial of surname (e.g., Erik Vils → EV) initials_to_check.append(firstname_upper[0] + surname_upper[0]) # 2. First three letters of first name (e.g., Erik → ERI) if len(firstname_upper) >= 3: initials_to_check.append(firstname_upper[:3]) # 3. Full first name (in case it's a key like "EVIL" in your dict) initials_to_check.append(firstname_upper) # 4. First initial + first two letters of surname (e.g., Erik Vils → EVI) if len(surname_upper) >= 2: initials_to_check.append(firstname_upper[0] + surname_upper[:2]) # Step 3: Find matching entries in the dictionary matching_entries = {} for initial in initials_to_check: if initial in dict1: # Remove the leading space from the dictionary value for cleaner output matching_entries[initial] = dict1[initial].strip() # Step 4: Display the results if matching_entries: print("Found matching entries:") for initial, full_name in matching_entries.items(): print(f"- '{initial}' → {full_name}") else: print("No matching initials found in the dictionary.")
Key Explanations:
- Input Standardization: Converting the input to uppercase ensures we match the dictionary's uppercase keys, even if the user types lowercase letters. Using
strip()removes any accidental extra spaces. - Generating Initials: We create multiple possible initial combinations to cover all the key formats in your dictionary (like
EV,EVI, etc.). You can add or remove combinations here based on what keys you expect in your dictionary. - Matching with Dictionary: We loop through our list of initials and check if each one exists as a key in
dict1. If it does, we store the corresponding full name in a new dictionary for easy output. - Result Display: Finally, we print out any matches we found, or a message if there are none.
Example Output:
If the user inputs Erik as the first name and Vils as the surname, the output will be:
Found matching entries: - 'EV' → Erik Vils - 'ERI' → Erik Vils - 'ERIK' → Erik Vils - 'EVI' → Erik Vils
(Note: In your original dictionary, EVIL is mapped to Erik Vils—if that's a typo for ERIK, you can update the dictionary key, and the code will automatically pick it up.)
内容的提问来源于stack exchange,提问作者Erik Vils

