0

I am trying to compare two lists ( which contain different sizes of sublists) and finding pair (enclosed in round bracket) that's not present in the other list.

Here is the code:

s1 = [
    [('RESOLVED - DUPLICATE', 'VERIFIED')], [('NEW', 'RESOLVED - FIXED'), ('RESOLVED - FIXED', 'VERIFIED')], [('NEW', 'RESOLVED - DUPLICATE'), ('RESOLVED - DUPLICATE', 'VERIFIED')], [('ASSIGNED', 'RESOLVED - FIXED'), ('RESOLVED - FIXED', 'VERIFIED')], [('NEW', 'RESOLVED - WONTFIX'), ('RESOLVED - WONTFIX', 'VERIFIED')], [('NEW', 'RESOLVED - INVALID'), ('RESOLVED - INVALID', 'VERIFIED')]
]

s2 = [
    [('RESOLVED - DUPLICATE', 'VERIFIED')], [('NEW', 'RESOLVED - DUPLICATE'), ('RESOLVED - DUPLICATE', 'VERIFIED')], [('NEW', 'ASSIGNED'), ('ASSIGNED', 'RESOLVED - FIXED')], [('ASSIGNED', 'RESOLVED - FIXED'), ('RESOLVED - FIXED', 'VERIFIED')], [('NEW', 'RESOLVED - WONTFIX'), ('RESOLVED - WONTFIX', 'VERIFIED')]
]

a = []

for item in s2:
    i = 0
    print item
    while (i < len(item)):
        for item1 in s1:
        print item[i]
        if item[i] not in s1:
            a.append(item[i])
        i = i + 1

print a

The above-mentioned code is not giving distinct pair that is present in s2 but not in s1.
Any help would be much appreciated. Thanks!

Abijith Mg
  • 2,647
  • 21
  • 35
user2293224
  • 2,128
  • 5
  • 28
  • 52

3 Answers3

2

Flatten your lists into set objects and the set-operations they impelement, in this case, you want the difference between s2 and s1,

>>> from itertools import chain
>>> set(chain.from_iterable(s2)).difference(chain.from_iterable(s1))
{('NEW', 'ASSIGNED')}

Note, you may actually want the symmetric difference between the two, i.e. the items that are not in both:

>>> set(chain.from_iterable(s1)).symmetric_difference(chain.from_iterable(s2))
{('NEW', 'RESOLVED - FIXED'), ('NEW', 'RESOLVED - INVALID'), ('NEW', 'ASSIGNED'), ('RESOLVED - INVALID', 'VERIFIED')}
juanpa.arrivillaga
  • 88,713
  • 10
  • 131
  • 172
  • Thanks for the solution. Is there any way of finding same pair using same way. I tried to look for equal or same function but didn't find it – user2293224 Nov 06 '17 at 03:35
  • @user2293224 yes, you mean the intersection. Like I mentioned, `set` objects implement set operations, i.e. intersection, union, difference, symmetric difference, ordering is on the subset relation. Check out [this answer to a similar question](https://stackoverflow.com/questions/46680450/how-to-return-sum-of-matching-elements-in-two-separate-sets/46680537#46680537) – juanpa.arrivillaga Nov 06 '17 at 04:05
  • @user2293224 here is some info in the [docs](https://docs.python.org/3/library/stdtypes.html#set-types-set-frozenset) – juanpa.arrivillaga Nov 06 '17 at 04:08
0

Assuming the nested structure is not relevant and the lists can be flattened:

s1flat = [x for sublist in s1 for x in sublist]
s2flat = [x for sublist in s2 for x in sublist]

new_pair = list(set(s2flat) - set(s1flat))
Robin Roth
  • 1,289
  • 10
  • 13
0

if you are finding items of s2 which are not present in s1 then use simply:

One line

print([item for item in s2 if item not in s1])

detailed:

That list comprehension is same as :

new_list=[]
for i in s2:
    if i not in s1:
        new_list.append(i)
print(new_list)

output:

[[('NEW', 'ASSIGNED'), ('ASSIGNED', 'RESOLVED - FIXED')]]
Aaditya Ura
  • 12,007
  • 7
  • 50
  • 88