Python Forum
comparing 2 dimensional list
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
comparing 2 dimensional list
#1
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
Reply
#2
first you didn't define i
you don't understand indexing. See: https://docs.python.org/3/tutorial/datastructures.html
Reply
#3
Im sorry im a noob in coding, i just cant figure it out , so that it will print "unique" in the output
Reply
#4
you can use:
if [1,2,3] in num1:
    print("it is")
else:
    print("Not found")
Reply
#5
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.
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply
#6
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
Reply
#7
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
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply
#8
(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!
Reply
#9
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
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply
#10
(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
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Comparing List values to get indexes Edward_ 7 1,138 Jun-09-2023, 04:57 PM
Last Post: deanhystad
  How to quantize a 4 dimensional array? PythonNPC 2 1,612 Apr-23-2022, 04:34 PM
Last Post: Gribouillis
  How to create 2 dimensional variables in Python? plumberpy 5 1,848 Mar-31-2022, 03:15 AM
Last Post: plumberpy
  Slicing a 2 dimensional array Scott 2 1,650 Jan-12-2022, 07:18 AM
Last Post: paul18fr
  Problem using two-dimensional interpolation. Result looks bad player1682 4 2,504 Oct-12-2021, 09:27 AM
Last Post: player1682
  Index data must be 1-dimensional : Classifier with sklearn Salma 0 4,311 Apr-01-2021, 03:22 PM
Last Post: Salma
  2 Dimensional Arrays Prithak 4 2,590 Mar-21-2021, 09:35 PM
Last Post: deanhystad
  Problem with comparing list palladium 2 2,147 Jul-08-2020, 10:49 AM
Last Post: DeaD_EyE
  Python 2 dimensional array creations sharpe 1 1,964 Nov-24-2019, 10:33 AM
Last Post: ThomasL
  Python: Creating N Dimensional Grid jf451 0 2,319 Dec-06-2018, 10:53 PM
Last Post: jf451

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020