我正在学习python的基础知识,并试图解决上述问题。 然而,我在这一点上陷入困境:当我修改代码时,一切似乎都可以,但是我得到一个错误:
print repr(string), numbers_in_lists(string) == result File "/tmp/vmuser_tmqtokdyaa/main.py", line 25, in numbers_in_lists while int(string[i]) <= prev: IndexError: string index out of range我修改了其他具有相同IndexError问题的线程,但这并没有很好地解决我的情况。 任何线索? 先谢谢你。
这里的任务是:
SeanMc从论坛中列出的数字定义了一个过程,该过程从1-9中接收一串数字,并输出一个带有以下参数的列表:字符串中的每个数字都应插入到列表中。 如果字符串中的数字x小于或等于前面的数字y,则应将数字x插入到子列表中。 继续将下列数字添加到子列表中,直到达到大于数字y的数字z。 然后将此数字z添加到正常列表并继续。
def numbers_in_lists(string): i=0 prev = 0 list = [] sublist = [] while i< len(string): if int(string[i]) > prev: list.append(string[i]) prev = int(string[i]) i +=1 else: while int(string[i]) <= prev: sublist.append(string[i]) i +=1 list.append(sublist) sublist = [] return list #testcases string = '543987' result = [5,[4,3],9,[8,7]] print repr(string), numbers_in_lists(string) == result string= '987654321' result = [9,[8,7,6,5,4,3,2,1]] print repr(string), numbers_in_lists(string) == result string = '455532123266' result = [4, 5, [5, 5, 3, 2, 1, 2, 3, 2], 6, [6]] print repr(string), numbers_in_lists(string) == result string = '123456789' result = [1, 2, 3, 4, 5, 6, 7, 8, 9] print repr(string), numbers_in_lists(string) == resultI am learning the basics of python, and trying to solve the above mentioned problem. However, I got stuck at this point: when I'm revising the code, everything seems OK to me, but I get an error:
print repr(string), numbers_in_lists(string) == result File "/tmp/vmuser_tmqtokdyaa/main.py", line 25, in numbers_in_lists while int(string[i]) <= prev: IndexError: string index out of rangeI did revise other threads with the same IndexError problem, but it didn't quite solve my situation here. Any clues? Thank you in advance.
Here goes the task:
Numbers in lists by SeanMc from forums define a procedure that takes in a string of numbers from 1-9 and outputs a list with the following parameters: Every number in the string should be inserted into the list. If a number x in the string is less than or equal to the preceding number y, the number x should be inserted into a sublist. Continue adding the following numbers to the sublist until reaching a number z that is greater than the number y. Then add this number z to the normal list and continue.
def numbers_in_lists(string): i=0 prev = 0 list = [] sublist = [] while i< len(string): if int(string[i]) > prev: list.append(string[i]) prev = int(string[i]) i +=1 else: while int(string[i]) <= prev: sublist.append(string[i]) i +=1 list.append(sublist) sublist = [] return list #testcases string = '543987' result = [5,[4,3],9,[8,7]] print repr(string), numbers_in_lists(string) == result string= '987654321' result = [9,[8,7,6,5,4,3,2,1]] print repr(string), numbers_in_lists(string) == result string = '455532123266' result = [4, 5, [5, 5, 3, 2, 1, 2, 3, 2], 6, [6]] print repr(string), numbers_in_lists(string) == result string = '123456789' result = [1, 2, 3, 4, 5, 6, 7, 8, 9] print repr(string), numbers_in_lists(string) == result最满意答案
发生这种情况的原因是,在这个内部,当你得到错误时,你不检查i < len(str)并且你继续递增,只检查与这个问题无关的条件,忽略你是否到达了字符串的最后一个字符或不。 我会改变它:
def numbers_in_lists(string): i=0 prev = 0 list = [] sublist = [] while i< len(string): if int(string[i]) > prev: list.append(string[i]) prev = int(string[i]) i +=1 else: while i< len(string) and int(string[i]) <= prev: sublist.append(string[i]) i +=1 list.append(sublist) sublist = [] return list我会考虑使用循环而不是在你的情况下,但上面的作品无论如何。
That happens because in this inner while, where you get the error, you don't check if i < len(str) and you keep incrementing, checking only condition unrelated to this problem, ignoring if you've reached last character of a string or not. I would change it to:
def numbers_in_lists(string): i=0 prev = 0 list = [] sublist = [] while i< len(string): if int(string[i]) > prev: list.append(string[i]) prev = int(string[i]) i +=1 else: while i< len(string) and int(string[i]) <= prev: sublist.append(string[i]) i +=1 list.append(sublist) sublist = [] return listAnd I would consider using for loop instead of while in your case, but above works anyway.
更多推荐
发布评论