我写了一个简单的程序来检查字符串是否是彼此的子串。问题是我不断得到列表索引超出界限错误。
我尝试在每次迭代时打印i和j,它们永远不会超出列表的范围。我甚至试图在s [5]和s [6]插入元素来检查索引,但仍然得到相同的错误。可能是造成此错误的原因是什么?
s = []
s.insert(0,str("a b c"))
s.insert(1,str("a b c d"))
s.insert(2,str("a b"))
s.insert(3,str("b c"))
s.insert(4,str("d"))
j = 0
i = 0
while j < 5:
if s[j] in s[i]:
print("\"" + s[j] + "\" is in the string \"" + s[i] + "\"")
i +=1
if i == 5 and j < 4:
j+=1
i=0
这是我的控制台输出
Traceback (most recent call last):
"a b c" is in the string "a b c"
File "C:/Users/Kal/PycharmProjects/untitled/FSS2.py", line 16, in <module>
"a b c" is in the string "a b c d"
if s[j] in s[i]:
"a b c d" is in the string "a b c d"
IndexError: list index out of range
"a b" is in the string "a b c"
"a b" is in the string "a b c d"
"a b" is in the string "a b"
"b c" is in the string "a b c"
"b c" is in the string "a b c d"
"b c" is in the string "b c"
"d" is in the string "a b c d"
"d" is in the string "d"
Process finished with exit code 1
答案 0 :(得分:1)
问题出在第18行
s = []
s.insert(0,str("a b c"))
s.insert(1,str("a b c d"))
s.insert(2,str("a b"))
s.insert(3,str("b c"))
s.insert(4,str("d"))
print(s)
j = 0
i = 0
while j < 5:
if s[j] in s[i]:
print("\"" + s[j] + "\" is in the string \"" + s[i] + "\"")
i +=1
if i == 5 and j < 4: <-- here
j+=1
i=0
在某些时候,您的i = 5
和j = 4
,因此此if i == 5 and j < 4
语句的右侧为False,而i
未重置为0.所以在下一个循环中,i
等于5,最大索引为4.
更好的解决方案是使用for循环。
s = []
s.insert(0,str("a b c"))
s.insert(1,str("a b c d"))
s.insert(2,str("a b"))
s.insert(3,str("b c"))
s.insert(4,str("d"))
for i in range(len(s)):
for j in range(len(s)):
if s[i] in s[j]:
print("\"" + s[i] + "\" is in the string \"" + s[j] + "\"")
s = []
s.insert(0,str("a b c"))
s.insert(1,str("a b c d"))
s.insert(2,str("a b"))
s.insert(3,str("b c"))
s.insert(4,str("d"))
j = 0
i = 0
while j < len(s):
if s[j] in s[i]:
print("\"" + s[j] + "\" is in the string \"" + s[i] + "\"")
i +=1
if i == len(s):
j+=1
i=0
答案 1 :(得分:0)
只有当i已经为5时,你才会增加j(注意if子句中的and
)。因此,当i = 5时,你仍处于while循环中(仅取决于j)并且你试图访问未定义的s [i] = s [5]。
答案 2 :(得分:0)
当您的代码引发异常时,i
的值为5
,j
的值为4.在print
语句中,您尝试要做s[i]
即s[5]
,并且由于s的最大索引为4
,因此您的代码正在提升IndexError
。
我相信,在您的代码中,您需要在if
语句中进行修改:
if i == 5 and j < 5: # Instead of j < 4
然后你的代码运行良好:
>>> while j < 5:
... if s[j] in s[i]:
... print("\"" + s[j] + "\" is in the string \"" + s[i] + "\"")
... i +=1
... if i == 5 and j < 5:
... j+=1
... i=0
...
"a b c" is in the string "a b c"
"a b c" is in the string "a b c d"
"a b c d" is in the string "a b c d"
"a b" is in the string "a b c"
"a b" is in the string "a b c d"
"a b" is in the string "a b"
"b c" is in the string "a b c"
"b c" is in the string "a b c d"
"b c" is in the string "b c"
"d" is in the string "a b c d"
"d" is in the string "d"
答案 3 :(得分:0)
while循环中的i和j变量不正确。更改值后,代码正常工作。
public class Test_finally {
private static int run(int input) {
int result = 0;
try {
result = 3 / input;
} catch (Exception e) {
System.out.println("UnsupportedOperationException");
throw new UnsupportedOperationException("first");
} finally {
System.out.println("finally input=" + input);
if (0 == input) {
System.out.println("ArithmeticException");
throw new ArithmeticException("second");
}
}
System.out.println("end of method");
return result * 2;
}
public static void main(String[] args) {
int output = Test_finally.run(0);
System.out.println(" output=" + output);
}
}
答案 4 :(得分:0)
保持你的形式:
s = []
s.insert(0,str("a b c"))
s.insert(1,str("a b c d"))
s.insert(2,str("a b"))
s.insert(3,str("b c"))
s.insert(4,str("d"))
j = 0
i = 0
while j < 5:
if s[j] in s[i]:
print("\"" + s[j] + "\" is in the string \"" + s[i] + "\"")
i +=1
if i == 5 and j <= 4:
j+=1
i=0
如果在while
循环中,则错误在第2位。它应该是j <= 4
或j < 5
才能发挥作用。