Python 索引超出范围解决方法
Python index out of range work-around
我是编码初学者。我正在寻找解决这个问题的方法:
我应该编写一个函数,该函数可以很好地获取一串带有单词和数字的文本,用空格分隔,如果连续有 3 个单词,则从该字符串输出 True。
示例:
'123 a b c' == True
'a 123 b c' == False
我尝试过的:
def 3_in_a_row(words):
words = words.split(" ")
for i in range(len(words)):
return words[i].isalpha() and words[i+1].isalpha() and words[i+2].isalpha()
如果我尝试这样做,我会得到一个 list index out of range
错误,因为当我接近列表末尾时,i
之后没有 2 个单词需要检查。
限制此功能的最佳方法是什么,以便在 i
之后没有 2 项可检查时停止?执行此操作的更好方法是什么?
您可以限制范围:
range(len(words) - 2)
因此它不会生成不能加 2 的索引。
但是,您的循环 return 太早了。您正在 return 测试 前 3 个单词 的结果。例如,您的测试将因 '123 a b c'
而失败,因为在第一次迭代中仅测试了 '123', 'a', 'b'
。将循环更改为:
def three_in_a_row(words):
words = words.split(" ")
for i in range(len(words) - 2):
if words[i].isalpha() and words[i+1].isalpha() and words[i+2].isalpha():
return True
return False
现在,如果您发现连续三个单词,这将 return 提早,并且只有扫描 所有单词 后,它才会宣告失败并 return False
.
一些其他提示:
您不能以数字开头 Python 标识符(如函数名称)。第一个字符 必须 是一个字母。我将上面的函数重命名为 three_in_a_row()
.
不带参数使用 words.split()
。在 任意空格 上拆分并忽略开头和结尾的空格。这意味着即使在某处之间不小心有 2 个空格,或者末尾有换行符或制表符,拆分也会起作用。
您可以使用 all()
function 来循环测试:
if all(w.isalpha() for w in words[i:i + 3]):
是拼写相同测试的一种更紧凑的方式。
具有这些更新的演示:
>>> def three_in_a_row(words):
... words = words.split()
... for i in range(len(words) - 2):
... if all(w.isalpha() for w in words[i:i + 3]):
... return True
... return False
...
>>> three_in_a_row('123 a b c')
True
>>> three_in_a_row('a 123 b c')
False
>>> three_in_a_row('a b c 123')
True
扩展关于all
用法的详细解答,这里是单行替代;
>>> l = '.. a b .. e f .. g h i'.split()
>>> pairs = [l[i:i + 3] for i in xrange(len(l))]
>>> result = any([all(c.isalpha() for c in pair) for pair in pairs if len(pair) == 3])
我是编码初学者。我正在寻找解决这个问题的方法: 我应该编写一个函数,该函数可以很好地获取一串带有单词和数字的文本,用空格分隔,如果连续有 3 个单词,则从该字符串输出 True。
示例:
'123 a b c' == True
'a 123 b c' == False
我尝试过的:
def 3_in_a_row(words):
words = words.split(" ")
for i in range(len(words)):
return words[i].isalpha() and words[i+1].isalpha() and words[i+2].isalpha()
如果我尝试这样做,我会得到一个 list index out of range
错误,因为当我接近列表末尾时,i
之后没有 2 个单词需要检查。
限制此功能的最佳方法是什么,以便在 i
之后没有 2 项可检查时停止?执行此操作的更好方法是什么?
您可以限制范围:
range(len(words) - 2)
因此它不会生成不能加 2 的索引。
但是,您的循环 return 太早了。您正在 return 测试 前 3 个单词 的结果。例如,您的测试将因 '123 a b c'
而失败,因为在第一次迭代中仅测试了 '123', 'a', 'b'
。将循环更改为:
def three_in_a_row(words):
words = words.split(" ")
for i in range(len(words) - 2):
if words[i].isalpha() and words[i+1].isalpha() and words[i+2].isalpha():
return True
return False
现在,如果您发现连续三个单词,这将 return 提早,并且只有扫描 所有单词 后,它才会宣告失败并 return False
.
一些其他提示:
您不能以数字开头 Python 标识符(如函数名称)。第一个字符 必须 是一个字母。我将上面的函数重命名为
three_in_a_row()
.不带参数使用
words.split()
。在 任意空格 上拆分并忽略开头和结尾的空格。这意味着即使在某处之间不小心有 2 个空格,或者末尾有换行符或制表符,拆分也会起作用。您可以使用
all()
function 来循环测试:if all(w.isalpha() for w in words[i:i + 3]):
是拼写相同测试的一种更紧凑的方式。
具有这些更新的演示:
>>> def three_in_a_row(words):
... words = words.split()
... for i in range(len(words) - 2):
... if all(w.isalpha() for w in words[i:i + 3]):
... return True
... return False
...
>>> three_in_a_row('123 a b c')
True
>>> three_in_a_row('a 123 b c')
False
>>> three_in_a_row('a b c 123')
True
扩展all
用法的详细解答,这里是单行替代;
>>> l = '.. a b .. e f .. g h i'.split()
>>> pairs = [l[i:i + 3] for i in xrange(len(l))]
>>> result = any([all(c.isalpha() for c in pair) for pair in pairs if len(pair) == 3])