jahnLudvik
jahnLudvik

Reputation: 93

find non common elements in two list of lists

I have two lists of lists like:

A = [[1,2,3],[1,5],[7],[8,9,10],[11]]
B = [[11,2,2],[9,11],[6,14], [17]]

and I would like to only extract sublists which don't have any element in common with the other list of lists, so in my case the result would be:

res = [[1,5],[7],[6,14],[17]

I would also like to extend the formula to more than 2 list of lists, if possible. I cannot find an easy way to do it, so that if I added another list of lists as in the example:

A = [[1,2,3],[1,5],[7],[8,9,10],[11]]
B = [[11,2,2],[9,11],[6,14], [17]]
C = [[17,18,19], [14,10],[100,101]]

than the result is:

res = [[1,5],[7],[100,101]]

Upvotes: 0

Views: 385

Answers (3)

cestlarumba
cestlarumba

Reputation: 198

I would create a function to do the job, then use reduce to apply the results to multiple lists.

from functools import reduce
from typing import List


def _find_not_common(list_a: List[List], list_b: List[List]):
    """"Returns sub lists in A that have no elements in present B"""
    flat_b = set([i for sub_list in list_b for i in sub_list])
    return [
        sub_list
        for sub_list
        in list_a
        if all([j not in flat_b for j in sub_list if j])
    ]


def find_not_common(list_a: List[List], list_b: List[List]):
    """"Returns sub lists in A that have no elements present in B + the viceversa"""
    return _find_not_common(list_a, list_b) + _find_not_common(list_b, list_a)


A = [[1, 2, 3], [1, 5], [7], [8, 9, 10], [11]]
B = [[11, 2, 2], [9, 11], [6, 14], [17]]
C = [[17, 18, 19], [14, 10], [100, 101]]

# Use reduce to pass a list of any length.
result = reduce(find_not_common, [A, B, C])  # Output: [[1, 5], [7], [100, 101]]

Upvotes: 2

Prothean
Prothean

Reputation: 26

You can try this one:

A = [[1,2,3],[1,5],[7],[8,9,10],[11]]
B = [[11,2,2],[9,11],[6,14], [17]]
C = [[17,18,19], [14,10],[100,101]]
lst=[A,B,C]
def non_common(ls):
    temp=[]
    for l in ls:
        without_l=[j for j in ls if j!=l] #get the list without the list-element we are iterating
        no_cmn=[i for i in l if (all(set(i).isdisjoint(set(j)) for k in without_l for j in k))]
        temp.extend(no_cmn)
    return temp
result=non_common(lst)
print(result)

You can also backtrack every list element to it's list by using enumerate(ls) in the loop.

Upvotes: 1

user7864386
user7864386

Reputation:

We could check the intersection between the union of sublists in B and each sublist in A (do the same job for each sublist in B and the union of sublists in A), then concatenate the resulting lists:

res = ([s_lst for s_lst in A if not set(s_lst).intersection(set().union(*B))] 
       + [s_lst for s_lst in B if not set(s_lst).intersection(set().union(*A))])

Output:

[[1, 5], [7], [6, 14], [17]]

For the more general case, one option is to create a dictionary and use the same idea as above in a loop:

lsts = [A,B,C]
d_unions = dict(enumerate([set().union(*X) for X in lsts]))
d_lsts = dict(enumerate(lsts))

out = []
for i, li in d_lsts.items():
    current_union = set.union(*[v for k,v in d_unions.items() if k!=i])
    out.extend([s_lst for s_lst in li if not set(s_lst).intersection(current_union)])

Output:

[[1, 5], [7], [100, 101]]

Upvotes: 2

Related Questions