简体   繁体   English

Python 2.7“列表索引超出范围”

[英]Python 2.7 “list index out of range”

I keep getting "IndexError: list index out of range", the code does fine with things like "s = 'miruxsexxzlbveznyaidekl'" but this particular length makes it throw an error. 我不断收到“ IndexError:列表索引超出范围”,代码可以很好地处理“ s ='miruxsexxzlbveznyaidekl'”之类的问题,但是此特定长度会引发错误。 Can anyone help me understand what I did wrong here, not just give me the answer? 谁能帮我了解我在这里做错的事情,而不仅仅是给我答案? (I'd like to not have to come back and ask more question haha) (我不想再问更多问题哈哈)

__author__ = 'Krowzer'

s = 'abcdefghijklmnopqrstuvwxyz'

def alpha(x):
    current_substring = []
    all_substring = []

    for l in range(len(x) - 1):
        current_substring.append(x[l])
        if x[l + 1] < x[l]:
            all_substring.append(current_substring)
            #print("current: ", current_substring)
            current_substring = []

    #print(all_substring)

    largest = all_substring[0]
    for i in range(len(all_substring)):
        if len(all_substring[i]) > len(largest):
            largest = all_substring[i]
    answer = ''.join(largest)
    print('Longest substring in alphabetical order is: ' + answer )

alpha(s)

I can try to explain what is going on. 我可以尝试解释发生了什么。

You are trying to find the longest substring in alphabetical order by looking for the end of the substring. 您正在尝试通过查找子字符串的结尾来按字母顺序找到最长的子字符串。 Your definition of end is that there is a character less than the last character in the string -- something in descending alphabetical order. 您对end的定义是,字符串中的字符少于最后一个字符-字母降序排列。

Your example substring has no such string. 您的示例子字符串没有这样的字符串。 So, the initial loop never finds an end to it. 因此,初始循环永远不会结束。 As a result, all_substring[] is empty and trying to get any element out of it (such as all_substring[0] ) generates an error. 结果, all_substring[]为空,尝试从中取出任何元素(例如all_substring[0] )会产生错误。

You can fix the code yourself. 您可以自己修复代码。 The easiest is probably just to check if it is empty. 最简单的方法可能只是检查它是否为空。 If so, then the entire original string is the match. 如果是这样,则整个原始字符串就是匹配项。

EDIT: 编辑:

On second thought, there are two errors in the code. 再三考虑,代码中有两个错误。 One is that the last character is not being considered. 一个是不考虑最后一个字符。 The second is that the final substring is not being considered. 第二个是不考虑最后的子字符串。

def alpha(x):
    current_substring = []
    all_substring = []

    for l in range(len(x)):
        current_substring.append(x[l])
        if l < len(x) - 1 and x[l + 1] < x[l]:
            all_substring.append(current_substring)
            #print("current: ", current_substring)
            current_substring = []

    print(all_substring)
    all_substring.append(current_substring)
    largest = all_substring[0]
    for i in range(len(all_substring)):
        if len(all_substring[i]) > len(largest):
            largest = all_substring[i]
    answer = ''.join(largest)
    print('Longest substring in alphabetical order is: ' + answer )

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM