Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 循环问题的索引器_Python_Loops_Camelcasing - Fatal编程技术网

Python 循环问题的索引器

Python 循环问题的索引器,python,loops,camelcasing,Python,Loops,Camelcasing,我很难将大小写字符串转换为单独的单词,并将它们添加到列表中。它几乎完成了代码,但给出了索引器:字符串索引超出范围。 请问有谁能帮忙吗?? 运行时的输出为: ['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]: Inde

我很难将大小写字符串转换为单独的单词,并将它们添加到列表中。它几乎完成了代码,但给出了索引器:字符串索引超出范围。 请问有谁能帮忙吗?? 运行时的输出为:

['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=“”
计数=0
字符=0
null=0
对于字符串[char:]中的i:
如果i==i.upper():
newstring=newstring+i
计数+=1
字符+=1
对于字符串[char]中的i:***<此处有错误**
如果i==i.upper()和char==1:
null+=1
elif i==i.lower():
newstring=newstring+i
字符+=1
计数+=1
elif i==i.upper()且计数>0:
List.append(新闻字符串)
打印(列表)
newstring=“”
打破

这是我完成这项任务的方法。我有一个名为lastword的计数器,它跟踪上一个“单词”的结束位置。然后我一个字母一个字母地循环整个字符串(就像你做的那样),如果我得到一个大写字母,那么你将lastword变量重置为当前位置

x = "ThisIsACamelCaseString"

#list to store results
sentence = []            
lastword = 0
#count keeps track of what position we are at
for count, letter in enumerate(x):
    #I use the isupper() methd to see if letter is uppercase
    if letter.isupper():
        word = x[lastword:count]
        lastword = count
        #if word is needed because otherwise the first 'word' will be literally nothing
        if word:
            sentence.append(word)
print sentence

它打印整个字符串,但不是实际字符串的最后一个单词“字符串”。这就像医生试图通过检查工作表上的孔来诊断疾病一样。我建议您在帖子中添加编辑,并使用与图片相反的代码进行更新。可以增加多少倍
char
?char增加16倍,得到16的值。然后在for循环的范围内设置该值。循环从位置16开始遍历字符串,但失败。这不应导致错误。它从字面上打印字符串中的所有单词,但在字符串的第16位与索引器崩溃。谢谢你。谢谢你的帮助。您是否有可能帮助我解决我的代码问题,因为我需要在本例中使用该特定版本。我尝试了您的代码版本,但它也没有打印最后一个单词,即字符串。您可以在for循环之后添加此行:
句。append(x[lastword:])
x = "ThisIsACamelCaseString"

#list to store results
sentence = []            
lastword = 0
#count keeps track of what position we are at
for count, letter in enumerate(x):
    #I use the isupper() methd to see if letter is uppercase
    if letter.isupper():
        word = x[lastword:count]
        lastword = count
        #if word is needed because otherwise the first 'word' will be literally nothing
        if word:
            sentence.append(word)
print sentence