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

数组字符串按特定标记拆分及格式规范问题排查(含大小写修正)

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

  1. Reversed Enumerate Variables: You wrote for letter, index in enumerate(s) but enumerate returns (index, value), so this swaps the index and the character. Your check for letter == '!' was actually comparing an integer (the index) to a string, so it never ran.
  2. Immutable String Operations: Methods like replace() or upper() return new strings—they don't modify the original list elements. You need to assign the result back to the list index.
  3. 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.
  4. 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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.08 09:37:29