如何基于特定标记拆分数组中的字符串

2024-09-24 22:29:09 发布

您现在位置:Python中文网/ 问答频道 /正文

我有下面的数据集,其中包含一些代码,用于识别字符串中的标记,以便正确拆分和格式化。我做错了什么?我似乎看不出我的错误所在

预期结果:

示例:

 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)
            




更新

我忘了包括'的'功能,我想帮助了

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

编辑

这是我的代码的一个非常微小的问题。在我的数据集的另一部分中,“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']


“和”应该变成“和”


Tags: and数据代码标记dataindexcounteranalysis
2条回答

我不确定你代码中的计数器和负计数器是用来做什么的。我排除了它们,因为它们不是获得你想要的结果所必需的

有几种不同的方法可以得到你想要的结果。这是一个我已经测试过的示例,并且将起作用。有关详细信息,请参阅我的内联注释

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)

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

下面是经过修改的代码。 PS:我在代码中使用了“加入”,您可以阅读:)

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)

相关问题 更多 >