Python Forum

Full Version: comparing 2 dimensional list
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2
i have this code:

 num1=[[1,2,3],[1,2,4],[1,2,5]] num2=[[1,2,3]] for i in num1: if (num2 == i): print("same")	else: print("unique")




whats wrong with my code?
num1 has 3 elements, num2 has single element with same value element of num1,
it should print "same" on output since num2 has a the same value element on num1
first you didn't define i
you don't understand indexing. See: https://docs.python.org/3/tutorial/datastructures.html
Im sorry im a noob in coding, i just cant figure it out , so that it will print "unique" in the output
you can use:
if [1,2,3] in num1: print("it is") else: print("Not found")
On row #5 You compare item (list) in first matrice (list of lists) with whole second matrice. They can’t be equal. You could use indexing to refer to item in second matrice (num2[0]) in comparison.
Thanks for your response,maybe comparing those 2 list is of no solution, in my code ,num2 data on it is changing, thats why in my mind i have to iterate num1 list of list , and compare num2 if there's the same value on num1
I probably don't understand the problem, but anyway:

>>> num1=[[1,2,3],[1,2,4],[1,2,5]] >>> num2=[[1,2,3]] >>> for i, item in enumerate(num1, start=1): ... if item == num2[0]: ... print(f'item no {i} is equal') ... else: ... print(f'item no {i} is not equal') ... item no 1 is equal item no 2 is not equal item no 3 is not equal
(Mar-24-2020, 12:13 PM)perfringo Wrote: [ -> ]I probably don't understand the problem, but anyway:

>>> num1=[[1,2,3],[1,2,4],[1,2,5]] >>> num2=[[1,2,3]] >>> for i, item in enumerate(num1, start=1): ... if item == num2[0]: ... print(f'item no {i} is equal') ... else: ... print(f'item no {i} is not equal') ... item no 1 is equal item no 2 is not equal item no 3 is not equal
thanks, this saves my day!
num2 should be a list and not a list in a list.

Your original code with the change:
num1 = [[1,2,3], [1,2,4], [1,2,5]] num2 = [1,2,3] for i in num1: if (num2 == i): print("same") else: print("unique")
Output:
same unique unique
(Mar-24-2020, 01:29 PM)DeaD_EyE Wrote: [ -> ]num2 should be a list and not a list in a list.

Your original code with the change:
num1 = [[1,2,3], [1,2,4], [1,2,5]] num2 = [1,2,3] for i in num1: if (num2 == i): print("same") else: print("unique")
Output:
same unique unique
Nothing has change i think
Pages: 1 2