下面的数据集带有一些代码,用于标识字符串中的标记,以便拆分并正确格式化。我做错了什么?我似乎看不出我的错误所在。
预期结果:
示例:
Dividing!polynomials --> Dividing Polynomials
Categorical!data!and!probabilities ---> Categorical Data and Probabilities“和”和"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‘功能,我也想要帮助它。
我需要包含某种标记,因为它是与其他代码集成所必需的。
编辑
这是我的代码中的一个非常微小的问题。在我的数据集的另一部分中,"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']和“应成为”和“”
发布于 2020-06-29 16:55:13
我不知道你代码中的计数器和否定计数器是用来做什么的。我消除了他们,因为他们没有必要得到你想要的结果。
有几种不同的方法可以得到你想要的结果。这是一个例子,我已经测试,并将发挥作用。有关详细信息,请参阅我的内联评论。
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)发布于 2020-06-29 17:14:59
您的代码没有什么问题。首先,您没有正确地使用“枚举”作为枚举解包,并给出元素和元素的索引。所以你得纠正一下。第二,你所写的语句(比如,替换语句或上面语句)并不影响列表。您需要显式地将这些更改分配给列表的相应索引,例如sindex = sindex.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)https://stackoverflow.com/questions/62641505
复制相似问题