【问题标题】:How to split up a string in an array based on a specific marker如何根据特定标记拆分数组中的字符串
【发布时间】:2020-10-19 19:34:43
【问题描述】:

我有下面的数据集和一些代码来识别字符串中的标记以正确拆分和格式化。我究竟做错了什么?我似乎看不到我的错误之处。

期望的结果:

例子:

 Dividing!polynomials --> Dividing Polynomials
 Categorical!data!and!probabilities ---> Categorical Data and Probabilities

“and”和“of”应始终小写

可重现的例子:

skilllist = ['Dividing!polynomials', 'Categorical!data!and!probabilities', 'Systems!of!linear!equations', 'Scatterplots!and!graphs']


for word in tempskilllist:
    s = list(word)
    
    for letter, index in enumerate(s):
        if letter == '!':
            counter = index + 1
            negcounter = index - 1
            if s[counter] == 'a':
                s[index].replace('!',' ')
            
            else:
                s[counter].upper()
                s[index].replace('!',' ')




print(tempskilllist)
            




更新

我忘了包含“of”功能,我也想为此提供帮助。

附:我需要包含某种标记,因为它是与其他代码集成所必需的。

编辑

这是我的代码的一个非常微小的问题。在我的数据集的另一部分,“and”中的“A”是大写的,我不知道如何使它成为小写的“a”。

数据集:


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']


“And”应该变成“and”

【问题讨论】:

    标签: python arrays for-loop replace format


    【解决方案1】:

    我不确定您的代码中的 counter 和 negcounter 是干什么用的。我消除了它们,因为它们不是获得您想要的结果所必需的。

    有几种不同的方法可以获得您正在寻找的结果。这是一个我已经测试过并且可以工作的例子。有关详细信息,请参阅我的内联 cmets。

    skillList = ['Dividing!polynomials', 'Categorical!data!and!probabilities', 'Systems!of!linear!equations', 'Scatterplots!and!graphs']
    
    skillsToSentence = []
    
    for word in skillList:
        sentence = word.replace("!", " ") # Replacing ! with spaces
        sentenceList = sentence.split() # Split will create list of words separating on spaces by default
        words = [] # List of words to store capitalized words
        for word in sentenceList:
            if word.lower() != "and" and word.lower() != "of": # Checking to see if the word needs to be title case, casting word to lower case
                word = word.title() # This Will Make The Word Title Case
                words.append(word)
            else: # If 'and' or 'or' is captialized in skillList, this will make sure it is always lower case
                word = word.lower() # this will make the word lower case
                words.append(word)
        joinedWords = " ".join(words) # Joining our list of words with a space between each element
        skillsToSentence.append(joinedWords) # Appending the string created in the line above to a final list
    
    print(skillsToSentence)
    

    【讨论】:

    • 查看我的编辑,使用 .lower() 将单词转换为小写将解决您的问题。发生这种行为是因为我的 if 语句只检查“和”。 'And' 或 'AND' 在我编辑之前使用 if 语句不会被捕获。
    【解决方案2】:

    您的代码几乎没有问题。 首先,您没有正确使用“枚举”,因为枚举解包并给出元素和元素的索引。因此,您必须进行更正。 其次,您正在编写的语句,例如,替换或上,不会影响列表。您需要将这些更改显式分配给列表的相应索引,例如 s[index] = s[index].replace('!', 'a')

    下面是您的修改后的代码。 PS:我在我的代码中使用了'join',你可以阅读它们:)

    tempskilllist = ['Dividing!polynomials', 'Categorical!data!and!probabilities', 'Systems!of!linear!equations',
                 'Scatterplots!and!graphs']
    new_tempskilllist = []
    
    for word in tempskilllist:
        s = list(word)
    
        for index, letter in enumerate(word):
            if letter == '!':
                counter = index + 1
                negcounter = index - 1
                if s[index] == 'a':
                    s[index] = s[index].replace('!', ' ')
    
                else:
                    s[index] = s[index].upper()
                    s[index] = s[index].replace('!', ' ')
        new_tempskilllist.append(''.join(s))
    
    print(new_tempskilllist)
    

    【讨论】:

    • 非常感谢兄弟!当出现另一个问题时,我对原始问题进行了编辑。你介意帮助我吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-11-18
    相关资源
    最近更新 更多