Python Forum
List comparison in Python - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: List comparison in Python (/thread-13061.html)



List comparison in Python - Nirmal - Sep-26-2018

HI

I have two list . i want to compare with each other with the list index[1][2][3] of each list with other list .If its a match then ignore , if not the return the whole list.

a = [['Eth1/1/13', 'Marketing', 'connected', '10', 'full', 'a-1000'], ['Eth1/1/14', 'NETFLOW02', 'connected', '10', 'full', '100']] b = [['Eth1/1/13', 'NETFLOW02', 'connected', '15', 'full', '100'], ['Eth1/1/14', 'Marketing', 'connected', '10', 'full', 'a-1000']]
Expected Output :

Diff a: Eth1/1/14 NETFLOW02 connected 10 full 100 Diff b: Eth1/1/13 NETFLOW02 connected 15 full 100
My Code :

p = [i for i in a if i not in b] for item in p: print item[0] print "\n++++++++++++++++++++++++++++++\n" q = [i for i in b if i not in a] for item in q: print item[0]
Any idea ?


RE: List comparison in Python - woooee - Sep-26-2018

Quote:p = [i for i in a if i not in b]
Print to see what it is doing
for item in a: print(item) print(item not in b) 
And you will have to be more specific about what is and is not a match. It looks like a date difference doesn't count but I am not going to guess.


RE: List comparison in Python - Nirmal - Sep-26-2018

i did research and manage to match list index[1] .. could not do for list index[2] and index[3]

c = [o for o in a if o[1] not in [n[1] for n in b]] print c
can we do similar match for o[2] and o[3] .. so that o[1],o[2],o[3] all should be checked in list b ?


RE: List comparison in Python - woooee - Sep-26-2018

If you aren't going to read what I post, there is no point to posting.
Quote:And you will have to be more specific about what is and is not a match. It looks like a date difference doesn't count but I am not going to guess



RE: List comparison in Python - nilamo - Sep-26-2018

(Sep-26-2018, 09:33 AM)Nirmal Wrote: list index[1][2][3] of each list with other list .If its a match then ignore , if not the return the whole list.
I don't know why you're trying to force a comprehension to do this. There's no need to build a new list if you're just returning the original one.

>>> def all_match(indices=(), *collections): ... collections = iter(collections) ... first = next(collections) ... val = None ... sub_list = first ... for index in indices: ... sub_list = sub_list[index] ... value = sub_list ... for items in collections: ... sub_value = None ... for index in indices: ... items = items[index] ... sub_value = items ... if sub_value != value: ... return False ... return first ... >>> a = [['Eth1/1/13', 'Marketing', 'connected', '10', 'full', 'a-1000'], ['Eth1/1/14', 'NETFLOW02', 'connected', '10', 'full', '100']] >>> ... b = [['Eth1/1/13', 'NETFLOW02', 'connected', '15', 'full', '100'], ['Eth1/1/14', 'Marketing', 'connected', '10', 'full', 'a-1000']] >>> all_match([1, 2, 3], a, b) [['Eth1/1/13', 'Marketing', 'connected', '10', 'full', 'a-1000'], ['Eth1/1/14', 'NETFLOW02', 'connected', '10', 'full', '100']] >>> a[1][2][3] 'n' >>> b[1][2][3] 'n' >>> a = [[''], ['', '', 'spam']] >>> a[1][2][3] 'm' >>> all_match([1, 2, 3], a, b) False



This forum uses Lukasz Tkacz MyBB addons.
Forum use Krzysztof "Supryk" Supryczynski addons.