数组字符串按特定标记拆分及格式规范问题排查(含大小写修正)
Hey there! Let's break down what's going wrong in your code and fix it step by step.
Key Issues in Your Original Code
- Reversed Enumerate Variables: You wrote
for letter, index in enumerate(s)butenumeratereturns(index, value), so this swaps the index and the character. Your check forletter == '!'was actually comparing an integer (the index) to a string, so it never ran. - Immutable String Operations: Methods like
replace()orupper()return new strings—they don't modify the original list elements. You need to assign the result back to the list index. - Incorrect Logic for Word Capitalization: Checking just the first letter after
!isn't enough to handle words like "and" or "of" properly; we need to process entire words instead of individual characters. - Missing Topiclist Processing: You didn't include any code to replace "And" with "and" in your topiclist.
Corrected Code with Explanations
First, let's handle the skilllist by splitting each string, normalizing word capitalization (with "and" and "of" always lowercase), then rejoining:
def process_skill(skill_str): # Split the string by '!' into individual words words = skill_str.split('!') processed_words = [] for word in words: # Keep "and" and "of" lowercase; capitalize other words if word.lower() in ['and', 'of']: processed_words.append(word.lower()) else: # Capitalize the first letter, lowercase the rest (in case of odd casing) processed_words.append(word.capitalize()) return ' '.join(processed_words) # Original skilllist skilllist = ['Dividing!polynomials', 'Categorical!data!and!probabilities', 'Systems!of!linear!equations', 'Scatterplots!and!graphs'] # Process each skill processed_skills = [process_skill(skill) for skill in skilllist] print("Processed Skill List:") for skill in processed_skills: print(f"- {skill}")
Next, let's fix the topiclist by replacing standalone "And" with "and":
def process_topic(topic_str): words = topic_str.split() processed_words = [] for word in words: # Replace exact "And" with lowercase "and" if word == 'And': processed_words.append('and') else: processed_words.append(word) return ' '.join(processed_words) # Original topiclist topiclist = ['Advanced Algebra', 'Problem Solving And Data Analysis', 'Basic Algebra', 'Problem Solving And Data Analysis', 'Advanced Algebra', 'Advanced Algebra', 'Advanced Algebra', 'Problem Solving And Data Analysis', 'Advanced Algebra', 'Problem Solving And Data Analysis', 'Problem Solving And Data Analysis', 'Problem Solving And Data Analysis', 'Advanced Algebra', 'Problem Solving And Data Analysis', 'Advanced Algebra', 'Advanced Algebra', 'Advanced Algebra', 'Advanced Algebra', 'Advanced Algebra', 'Problem Solving And Data Analysis'] # Process each topic processed_topics = [process_topic(topic) for topic in topiclist] print("\nProcessed Topic List:") for topic in processed_topics: print(f"- {topic}")
Output
Processed Skill List: - Dividing Polynomials - Categorical Data and Probabilities - Systems of Linear Equations - Scatterplots and Graphs Processed Topic List: - Advanced Algebra - Problem Solving and Data Analysis - Basic Algebra - Problem Solving and Data Analysis - Advanced Algebra - Advanced Algebra - Advanced Algebra - Problem Solving and Data Analysis - Advanced Algebra - Problem Solving and Data Analysis - Problem Solving and Data Analysis - Problem Solving and Data Analysis - Advanced Algebra - Problem Solving and Data Analysis - Advanced Algebra - Advanced Algebra - Advanced Algebra - Advanced Algebra - Advanced Algebra - Problem Solving and Data Analysis
This code is more readable, handles edge cases (like unexpected casing in words), and achieves exactly what you need.
内容的提问来源于stack exchange,提问作者user13824876
相关产品推荐
相关产品推荐

