I want to explain my code to you.

1. this code is to search for the given number whether it is present in the list or not.

2. first if statement returns the value of row if the i/p number is in the first column

3. if the given number is not in the first column, the second if statement compares the i/p number with the values in the first column of different rows.

4. if the i/p number is less than the value of matrixM[row+1][0], it goes to
matrixM[row][0] i.e. previous row to search the i/p number and return index.

Suppose the i/p number is 22, since it is not in the first column, the second if statement is executed.
when row = 2, row+1 = 3 =>matrixM[3][0] == 28, since 22<28, it should go to the third row and search for 22 & return the value of row and column.

def checkvalue(number,matrixM):
	
	for row in range(len(matrixM)):
		if (matrixM[row][0] == number):
			return row,0
		
		elif(matrixM[row+1][0] < number):   ##line 30
			for column in range(len(matrixM)):
				if(matrixM[row][column] == number):
					return row, column
			  
					
				
def main():
	matrix = [[2,4,6,8],[10,12,14,16],[20,22,24,26],[28,30,32,34]]
	row1=0
	column1=0	
	n = int(input(('provide the value of n: ')))
	row1,column1 = checkvalue(n,matrix) ##line 42
	print('\n')
	print(row1,column1)
	return 0

if __name__ == '__main__':
	main() ##line48

provide the value of n: 22
Traceback (most recent call last):
File "check.py", line 48, in <module>
main()
File "check.py", line 42, in main
row1,column1 = checkvalue(n,matrix)
File "check.py", line 30, in checkvalue
elif(matrixM[row+1][0] < number):
IndexError: list index out of range

why is this showing error in main()

Do you mean:

>>> matrix = [[2,4,6,8],[10,12,14,16],[20,22,24,26],[28,30,32,34]]
>>> print any(28 in sub for sub in matrix)
True

This only return the truth value if it is in the list like you discribe as your point one. However it looks like you want to actually return the row, column:

>>> print [(a,b) for a, sub in enumerate(matrix) for b, v in enumerate(sub) if v==28]
[(3, 0)]
>>> print matrix[3][0]
28
>>>

Based on the sample data you provided, it would be simpler to find the row that starts with a number greater than the one you are looking for and check the previous row.

def compare_columns(matrix, number):
    for row in range(1, len(matrix)):
        if number <= matrix[row][0]:
            ## number is less than first element so check previous row
            check_row = row-1
            for col in range(len(matrix[check_row])):
                if number == matrix[check_row][col]:
                    return check_row, col
            return -1, -1     ## not found in this row

    ## last row
    row = len(matrix)-1
    for col in range(len(matrix[-1])):
        if matrix[row][col]==number:
            return row, col
    return -1, -1    ## not found
 
matrix = [[2,4,6,8],[10,12,14,16],[20,22,24,26],[28,30,32,34]]
print compare_columns(matrix, 22)
print compare_columns(matrix, 34)
print compare_columns(matrix, 23)

Edited 4 Years Ago by woooee: n/a

File "check.py", line 30, in checkvalue
elif(matrixM[row+1][0] < number):
IndexError: list index out of range

I did not state the reason for the range(1, len(matrix)) You get the error message when you access the last row
for row in range(1, len(matrix)):
and then use
elif(matrixM[row+1][0] < number)
i.e row+1 does not exist when you are on the final row. Instead you can iterate through row-1 with your code, or start at 1, not zero, through the last row, and look at row-1 (check_row = row-1) as in my code
for row in range(1, len(matrix)):

Also there is an error in the code above
if number <= matrix[row][0]:
should read
if number < matrix[row][0]:
Testing usually catches some mistake.

Edited 4 Years Ago by woooee: n/a

Thanks to you both woooee and pyTony for the help.

I've solved the problem.

def checkvalue(number,matrixM):
	for row in range(len(matrixM)):
		print(len(matrixM))
		if (matrixM[row][0] == number):
			return row,0
		
		
	for row in range(len(matrixM)):
		if(matrixM[row][0] >= number):
			for column in range(len(matrixM)):
				if(matrixM[row-1][column] == number):
					return row-1, column 
		
		if(number > matrixM[3][0]):
			for column in range(len(matrixM)):
				if(matrixM[3][column] == number):
					return 3, column
				
	return -1,-1
		
def main():
	matrix = [[2,4,6,8],[10,12,14,16],[20,22,24,26],[28,30,32,34]]
	n = int(input(('provide the value of n: ')))
	row1,column1 = checkvalue(n,matrix)
	print('\n')
	print(row1,column1)
	return 0

if __name__ == '__main__':
	main()

If I had used the general searching algorithm i.e. search every index one by one it had taken m*n time.

Now how to write the code to check the time taken by this code to search for a particular value?

This code will test every row up to and including the correct row.

for row in range(len(matrixM)):
		if(matrixM[row][0] >= number):  ## greater than all lower rows

If you have a large table this will affect the time it takes. It appears you still have some test code in the program

if(number > matrixM[3][0]):
			for column in range(len(matrixM)):
				if(matrixM[3][column] == number):
					return 3, column

In any case it should not be under the for() and execute on every pass through the loop. The best way to check times is to use a profiler on the code. It will tell you how much time was spent in each part of the code.

how can I get the output of this code to XML file? Can anyone guide me with this problem?

This article has been dead for over six months. Start a new discussion instead.