我很难将驼峰字符串转换为单独的单词并将它们附加到列表中。它几乎完成了代码,但它提供了一个IndexError:字符串索引超出范围。 请任何人都可以帮忙吗? 输出运行时为:indexerror for循环问题
['This']
['This', 'Is']
['This', 'Is', 'A']
['This', 'Is', 'A', 'Camel']
['This', 'Is', 'A', 'Camel', 'Case']
Traceback (most recent call last):
for i in string[char]:
IndexError: string index out of range
List = []
string = "ThisIsACamelCaseString"
newstring = ""
count = 0
char = 0
null = 0
for i in string[char:]:
if i == i.upper():
newstring = newstring + i
count += 1
char += 1
for i in string[char]: **< error here**
if i == i.upper() and char == 1:
null += 1
elif i == i.lower():
newstring = newstring + i
char += 1
count += 1
elif i == i.upper() and count > 0:
List.append(newstring)
print(List)
newstring = ""
break
它打印整个字符串,但不是最后的“字符串”从实际字符串 –
这就像一个医生试图通过在纸张上的孔寻求诊断的疾病。 –
我建议在您的文章中添加修改,并使用与图片相对的代码进行更新。 – GedAWizardofEarthSea