def LCS(word_list1, word_list2):
m = len(word_list1)
n = len(word_list2)
print m
print n
C = [[0] * (n+1) for i in range(m+1)] # IndentationError: unindent does not match any outer indentation level
print C
i=0
j=0
for word in word_list1:
j=0
for word in word_list2:
if word_list1[i-1] == word_list2[j-1]:
C[i][j] = C[i-1][j-1] + 1
else:
C[i][j] = max(word_list1(C[i][j-1], C[i-1][j]))
j+=1
i+=1
return C
views:
116answers:
3These two lines
C = [[0] * (n+1) for i in range(m+1)] # IndentationError: unindent does not match any outer indentation level
print C
should be indented at the same level. I.e.:
C = [[0] * (n+1) for i in range(m+1)]
print C
Update
Op has corrected the above problem. I checked the code and the error is elsewhere now:
for word in word_list2:
if word_list1[i-1] == word_list2[j-1]:
C[i][j] = C[i-1][j-1] + 1
else:
C[i][j] = max(word_list1(C[i][j-1], C[i-1][j]))
j+=1
Should be:
for word in word_list2:
# These lines have been indented extra.
if word_list1[i-1] == word_list2[j-1]:
C[i][j] = C[i-1][j-1] + 1
else:
C[i][j] = max(word_list1(C[i][j-1], C[i-1][j]))
j+=1
It's hard to answer a question on why indentation is incorrect when the question keeps being edited and the indentation corrected.
However, I suggest you read PEP8 before writing any more Python code and avoid mixing tabs and spaces. This would explain why you still see an IndentationError
on line seven after you have corrected the indentation.
I also recommend that you try to run your script using the '-tt
' command-line option to determine when you accidentally mix tabs and spaces. Of course any decent editor will be able to highlight tabs versus spaces (such as Vim's 'list'
option).
I think its due use of mixed indentation - Tabs and Spaces
Recommendation: Use 4 spaces per indentation level,
Never mix tabs and spaces.
Maximum Line Length: 80
Check your Python - Editor's Setting,
GEdit:
From Tools -> Preferences -> Editor -> Tab Width = 4 and use Spaces instead of Tabs
Eclipse:
use Pydev - http://pydev.org
VIM:
use following vim settings - for vim editor
:set tabstop=4 expandtab shiftwidth=4 softtabstop=4