0

I have two list . i want to compare with each other with the list index[1][2][3] of "a" of each list with other list index[1][2][3] of "b" .If its a match then ignore , if not then 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']]

Desired Output :

Diff a:

Eth1/1/14  NETFLOW02   connected    10   full    100

Diff b:

Eth1/1/13  NETFLOW02    connected    15   full    100

What i am trying :

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]

tried below but only managed to match index 1 of inner list , index 2 and 3 still need to be matched..

[o for o in a if o[1] not in [n[1] for n in b]

I am not getting the expected output.Any idea how to do this ?

  • https://stackoverflow.com/questions/6486450/python-compute-list-difference – Carlo 1585 Sep 26 '18 at 16:37
  • What output do you get? – Swift Sep 26 '18 at 16:38
  • 1
    *"I am not getting the expected output."* What is the error? What output did you get? – Austin Sep 26 '18 at 16:38
  • 1
    I doubt you mean `a[1][2][3]`, which would return `'n'`, do you mean index 1, 2 and 3? – rahlf23 Sep 26 '18 at 16:39
  • You want to compare items one, two, and three of the **inner** lists? Your solution is comparing *complete* **inner** lists, I don't see any any indexing or slices. – wwii Sep 26 '18 at 16:40
  • for e:g 'Marketing', 'connected', '10' of a[1] need to be compared with b[1] and b[2] and so on..if all are matched then ignore..if not matched then return the whole list.. – Nirmal Gauda Sep 26 '18 at 16:44
  • Edited my answer, sorry for the first one. I misunderstood the question entirely lol. – Swift Sep 26 '18 at 20:28

2 Answers2

0
for sublista in a:
    if not any(sublista[1:4] == sublistb[1:4] for sublistb in b):
        print(sublista)

You need an inner loop so that each sub-list from list a can be compared to each sub-list in list b. The inner loop is accomplished with a generator expression. Slices are used to to compare only a portion of the sub-lists. The built-in function any consumes the generator expression; it is lazy and will return True with the first True equivalency comparison. This will print each sub-list in a that does not have a match in b - to print each sub-list in b that does not have a match in a, put b in the outer loop and a in the inner loop.

Here is an equivalent Without using a generator expression or any:

for sublista in a:
    equal = False
    for sublistb in b:
        if sublista[1:4] == sublistb[1:4]:
            break
    else:
        print(sublista)

Sometimes it is nice to use operator.itemgetter so you can use names for the slices which can make the code more intelligible.:

import operator
good_stuff = operator.itemgetter(1,2,3)
for sublista in a:
    if not any(good_stuff(sublista) == good_stuff(sublistb) for sublistb in b):
        print(sublista)

itertools.product conveniently generates pairs and can be used as a substitute for the nested loops above. The following uses a dictionary (defaultdict) to hold comparison results for each sublist in a and b, then checks to see if there were matches - it does both the a to b and b to a comparisons.

import itertools, collections
pairs = itertools.product(a, b)
results = collections.defaultdict(list)
for sub_one, sub_two in pairs:
    comparison = good_stuff(sub_one) == good_stuff(sub_two)
    results[tuple(sub_one)].append(comparison)
    results[tuple(sub_two)].append(comparison)

for sublist, comparisons in results.items():
    if any(comparisons):
        continue
    print(sublist)

# or
from pprint import pprint
results = [sublist for sublist, comparisons in results.items() if not any(comparisons)]
pprint(results)
wwii
  • 23,232
  • 7
  • 37
  • 77
0
for v in a,b:
    for items in v:
        if 'NETFLOW02' in items:
            print('\t'.join(items))

I'm not sure this is ok for your purpose but you seems to want to capture the results of a network interface called NETFLOW02 from these two lists.

I'm sure there's probably a reason this is unacceptable but you could also expand this to include other keywords in longer lists, well, any length of lists that are nested as far as explained in your question. To do this, you would need to create another list, hypothetically keywords = ['NETFLOW02','ETH01']

Then we simply iterate this list also.

results = []
for v in a,b:
    for item in v:
        for kw in keywords:
            if kw in item:
            results.append(item)
            print('\t'.join(item))
print(results)
Swift
  • 1,663
  • 1
  • 10
  • 21