Comparing Columns Function
I am trying to write a function in Python that takes in two inputs: a single list from a list of lists (called row) a 2D array formatted as a list of lists (called A). I want
Solution 1:
maybe like this
defcompcols(row, A):
for i in A:
if row[0]==i[0]:
for j, k inzip(row, i):
if j==0and k!=0:
print row
Solution 2:
So you get a list index is out of range of that line if row[j]==0 and A[i][j]!=0:
. j
is by construction in range(len(row))
so the error should not come from row[j]
. On previous line, you allready have if row[0]==A[i][0]:
so it should not come from A[i].
I suspect it comes from A[i]
having less elements that row
. You should add a test to be sure :
defcompcols(row, A):
for i inrange(len (A)):
if (len(A[i]) < len(row):
raise Exception("Line %d len(A[i]) %d - len(row) %d"
% (i, len(A[i]), len(row)))
for j inrange(len(row)):
if row[0]==A[i][0]:
if row[j]==0and A[i][j]!=0:
print row
Post a Comment for "Comparing Columns Function"