Python While 循环中的范围错误
Python Range Error in While Loop
我正在尝试编写一个 python 程序,它将接受任何小写字母字符串和 return 其中最长的字母子字符串。下面是一段代码。
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+shift+expan] <= s[1+shift+expan]:
创建一个字符串索引超出范围的错误。我看到添加到 expan 会使索引超出范围,但最大的 while 循环不应该解决这个问题吗?我感谢任何帮助。
看看这个。
>>> 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
首先,为什么你的代码不起作用:
- 你没有保护你的内部循环免受字符串末尾 运行 的影响
- 当 "saving" 子串
时你的索引关闭
- 你
+=
到 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']
所以最后一步是找到最长的那个,我会留给你,因为这看起来像作业。
回答问题:
I see that adding to expan will put the index out of range, but shouldn't the largest while loop solve this?
它可以防止您的起始子字符串索引失效,但不能防止您的扩展。您必须防范这两种可能性。
我正在尝试编写一个 python 程序,它将接受任何小写字母字符串和 return 其中最长的字母子字符串。下面是一段代码。
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+shift+expan] <= s[1+shift+expan]: 创建一个字符串索引超出范围的错误。我看到添加到 expan 会使索引超出范围,但最大的 while 循环不应该解决这个问题吗?我感谢任何帮助。
看看这个。
>>> 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
首先,为什么你的代码不起作用:
- 你没有保护你的内部循环免受字符串末尾 运行 的影响
- 当 "saving" 子串 时你的索引关闭
- 你
+=
到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']
所以最后一步是找到最长的那个,我会留给你,因为这看起来像作业。
回答问题:
I see that adding to expan will put the index out of range, but shouldn't the largest while loop solve this?
它可以防止您的起始子字符串索引失效,但不能防止您的扩展。您必须防范这两种可能性。