Posts: 27 Threads: 11 Joined: Mar 2020 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 Posts: 12,117 Threads: 494 Joined: Sep 2016 first you didn't define i you don't understand indexing. See: https://docs.python.org/3/tutorial/datastructures.html Posts: 27 Threads: 11 Joined: Mar 2020 Im sorry im a noob in coding, i just cant figure it out , so that it will print "unique" in the output Posts: 12,117 Threads: 494 Joined: Sep 2016 you can use: if [1,2,3] in num1: print("it is") else: print("Not found") Posts: 1,953 Threads: 8 Joined: Jun 2018 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. Posts: 27 Threads: 11 Joined: Mar 2020 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 Posts: 1,953 Threads: 8 Joined: Jun 2018 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. Posts: 27 Threads: 11 Joined: Mar 2020 (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! Posts: 2,171 Threads: 12 Joined: May 2017 Mar-24-2020, 01:29 PM (This post was last modified: Mar-24-2020, 01:30 PM by DeaD_EyE.) 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 Posts: 27 Threads: 11 Joined: Mar 2020 (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 |