2016-10-02 26 views
0

我寫了一個簡單的程序來檢查字符串是否是彼此的子字符串。問題是我不斷收到列表索引超出界限的錯誤。列表索引超出範圍,雖然看起來不是

我試着在每次迭代時打印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

請注意,要添加在列表的末尾,您應該只是'.append'。 – jonrsharpe

+0

'print(i,j)'在你循環的開始,它會很清楚。你應該檢查'如果我== 4和j <4:' –

+0

@PadraicCunningham我嘗試使用該檢查,但我仍然得到相同的錯誤 – user6912880

回答

0

在點當你的代碼是提高外,中i5j值爲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" 
+0

如果這不是所需的輸出,請用您希望的輸出更新問題。因爲我很難猜測你的代碼應該執行什麼。 –

+0

這確實是所需的輸出(我用它來修復用戶輸入子串檢查器的邏輯錯誤) – user6912880

0

你似乎j增加,只有當我已經是5(注意,如果子句中的and)。因此,當i = 5時,你仍然處於while循環(它只取決於j),並且嘗試訪問未定義的s [i] = s [5]。

+0

Python不是Javascript,沒有未定義的,它是一個IndexError。 –

+0

對,但我不是說我[5]擁有「未定義」的值。只有那沒有與它相關的價值。 – imant

+0

我這樣做,以便每個項目與其他人進行檢查。不應該這是一個非問題,因爲在我被用作索引之前,它被重置爲0?另外當我在s [5]和s [6]中添加元素時,這不應該消除錯誤嗎? – user6912880

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 = 5j = 4,所以這個if i == 5 and j < 4語句的右側爲False,並且i沒有那麼在下一個循環reseted爲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

或者,您可以使用列表理解並在最後打印列表。 (len(s))中的範圍(len(s))中的i的格式(s [j],s [i])if(012) s [j] in s [i]]' – TheF1rstPancake

+1

有一個原因,爲什麼我沒有試圖徹底改變正在學習的人的代碼。很明顯,我知道如何使用列表理解或使用現有的庫,但重要的是他沒有。我認爲最好這樣做。 – Nf4r

+0

沒有試圖說你的解決方案是錯誤的 - 這就是我留下評論的原因。只是另一種做法。 – TheF1rstPancake

0

你的i和j變量在while循環是不正確的。在代碼運行後更改值後。

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 == 4 and j < 5: 
     j+=1 
     i=0 
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循環。它應該是j <= 4j < 5才能工作。