2017-06-08 142 views
1

我正在尝试编写一个Python程序,该程序将采用任何小写字母的字符串并返回其中最长的字母顺序字符串。以下是一段代码。While循环中的Python范围错误

s="abc"           #sample string 
anslist=[]           #stores answers 
shift=0           #shifts substring 
expan=0           #expands substring 
while len(s) >= 1+shift+expan:      #within bounds of s 
    if s[0+shift+expan] > s[1+shift+expan]:  #if not alphabetical 
     shift += 1         #moves substring over 
    else:           #if alphabetical 
     while s[0+shift+expan] <= s[1+shift+expan]: #while alphabetical 
      expan += 1        #checks next letter 
     anslist += s[0+shift:2+shift+expan]  #adds substring to ans 
     expan = 0         #resets expansion 

当运行该代码,含有 而S [0 +移+ EXPAN] < = S [1 +移+ EXPAN]的行: 创建错误该字符串索引是该范围以外的。我发现加入expan会使索引超出范围,但不应该用最大的while循环解决这个问题吗?我感谢任何帮助。

+0

这很可能是一次错误。因为len将比最后一个基于0的索引大1,所以while条件不应该是len(s)> 1 + shift + expan吗? –

+0

@ 0101001101000010这不会解决它仍然会出界的错误。 's'有一个len长度为'3',但由于最大索引是'len(object)-1'和'shift + expan> 0',所以你只能索引到2。 – Pythonista

+0

'S [0 +移+ EXPAN] <= S [1 +移+ EXPAN]'是对于b真和c,从而然后EXPAN是+ = 1,并且超出了范围,应添加像'的检查和扩大+ 1 + 1 + shift depperm

回答

1

首先,为什么你的代码不起作用:

  • 你是不是保护您的内环反对跑掉字符串末尾
  • “保存”子字符串时,您的索引已关闭
  • +=anslist,这是不是你的字符串添加到列表
  • 你不处理子后递增shift,所以当它清除expan它相同指数重新开始,并永远循环

固定码(内联注释解释的变化):

s="abckdefghacbde"         #sample string 
anslist=[]           #stores answers 
shift=0           #shifts substring 
expan=0           #expands substring 
while len(s) > 1+shift+expan:      #within bounds of s 
    if s[0+shift+expan] > s[1+shift+expan]:  #if not alphabetical 
     shift += 1         #moves substring over 
    else:           #if alphabetical 
     # Added guard for end of string 
     while len(s) > 1 + shift + expan and  # While still valid 
       s[0+shift+expan] <= s[1+shift+expan]:# While alphabetical 
      expan += 1        #checks next letter 
     # Fixed string sublength and append instead of += 
     anslist.append(s[0+shift:1+shift+expan]) #adds substring to ans 
     # Continue to next possible substring 
     shift += expan        # skip inner substrings 
     expan = 0 
print anslist 

结果:

['abck', 'defgh', 'ac', 'bde'] 

所以最后一步是找到长度最长的那一个,我将留给你,因为这看起来像作业。

要回答这个问题:

我看到增加EXPAN将使指数超出范围,但不应最大while循环解决这个问题?

它可以防止您的起始子字符串索引脱离,但不会扩展。你必须保护两种可能性。

+0

我已经在这段代码上工作了两天,很高兴终于找到错误是什么。非常感谢您的详细回复,并感谢大家对这个问题的贡献! –

+0

@GradyMorrissey未来,请包含堆栈跟踪,因为它可以更容易地识别具体问题。另外,我建议你先看看[PEP-8](https://www.python.org/dev/peps/pep-0008/),因为有一些重要的样式问题会让你的代码难以阅读。话虽如此,不要气馁。每个人都会犯错。 – TemporalWolf

0

看看这个。

>>> import re 
>>> words = [] 
>>> word = r'[a]*[b]*[c]*[d]*[e]*[f]*[g]*[h]*[i]*[j]*[k]*[l]*[m]*[n]*[o]*[q]*[r]*[s]*[t]*[u]*[v]*[x]*[y]*[z]*' # regex that would match sub-strings. Just extend it up to z. 
>>> string = "bacde" 
>>> for m in re.finditer(word, string): 
...   words.append(m.group()) 
>>> print(words) # list of substrings 
['b', 'acde'] 

然后你就可以从字符串列表中提取最大的字符串

>>> print(max(words, key=len)) 
acde 
+0

我认为他需要像''abc“'或'”def“'这样的字母,而不仅仅是字母字符。 – TemporalWolf

+0

看到他说按字母顺序排列的子串。我评论说他不清楚他想要什么。他应该给一个预期的输出abc不会削减! – Djokester

+1

@Djokester对不起,我感到困惑。对于s =“abc”,预期的输出是“abc”。同样,对于s =“bac”,预期输出为“ac”。 –