shantanuo
shantanuo

Reputation: 32296

compare lists and find combinations

I have 3 lists and I am trying to find matching combinations.

mylist1 = ["a", "b", "c", "d", "e", "f", "x", "y", "p"]
mylist2 = ["a", "b", "c", "d", "p", "q"]
mylist3 = ["a", "b", "c", "d", "e", "f", "g", "h", "q"]

g, h, x and y do not have any match so I will discard them. The result ["a", "b", "c" ] 3 is valid but I need to discard that as well because that is the subset of ["a", "b", "c", "d"] 3 The expected output is:

["a", "b", "c", "d"] 3
["a", "b", "c", "d", "e", "f"] 2
["a", "b", "c", "d", "p"] 2
["a", "b", "c", "d", "q"] 2

Upvotes: 1

Views: 205

Answers (4)

user0
user0

Reputation: 138

import itertools

def main(T0:list[set]):
    for i in range(2,len(T0)+1):
        for o1 in itertools.combinations(T0,i):
            t1=o1[0].copy()
            for o2 in o1[1:]:
                t1&=o2
            n1=0
            for o3 in T0:
                n1+=t1.issubset(o3)
            print(t1,n1)


main([
    {"a", "b", "c", "d", "e", "f", "x", "y", "p"},
    {"a", "b", "c", "d", "p", "q"},
    {"a", "b", "c", "d", "e", "f", "g", "h", "q"},
    ])

Upvotes: 0

Anna Semjén
Anna Semjén

Reputation: 787

Supposing you requirement is: You don't want to see anything which occurs only once - But only want to display anything that is at least common in two of your lists.

First you need to figure out how many combinations you can choose from your lists. Here you have 3 lists --> that is 4 combinations - itertools.combinations can help with that

Then you need to define the combinations and intersect them one by one see it below:

import itertools
from functools import reduce

mylist1 = ["a", "b", "c", "d", "e", "f", "x", "y", "p"]
mylist2 = ["a", "b", "c", "d", "p", "q"]
mylist3 = ["a", "b", "c", "d", "e", "f", "g", "h", "q"]

def definer(*args):
    # Number of lists for input
    counter = len(args)
    my_outputs = []
    # Only collecting where values are at least in two lists:
    for i in range(2, counter+1):
        x = (g for g in itertools.combinations(args, i))
        for item in x:
            result = reduce(set.intersection, (set(a) for a in item))

            my_outputs.append([sorted(list(result)), i])
    return my_outputs

print(definer(mylist1,mylist2,mylist3))

Upvotes: 2

ppwater
ppwater

Reputation: 2277

This will get you the expected output

mylist1 = ["a", "b", "c", "d", "e", "f", "x", "y", "p"]
mylist2 = ["a", "b", "c", "d", "p", "q"]
mylist3 = ["a", "b", "c", "d", "e", "f", "g", "h", "q"]

s1 = set(mylist1)
s2 = set(mylist2)
s3 = set(mylist3)

print(sorted(list(s1.intersection(s2).intersection(s3))), 3)
print(sorted(list(s1.intersection(s3))), 2)
print(sorted(list(s1.intersection(s2))), 2)
print(sorted(list(s2.intersection(s3))), 2)

First, convert the list to set. then do intersection with the sets, then convert that into the list, then sort it.

Upvotes: 0

mujjiga
mujjiga

Reputation: 16856

s1 = set(mylist1)
s2 = set(mylist2)
s3 = set(mylist3)

print (s1.intersection(s2).intersection(s3), 3)
print (s1.intersection(s2), 2)
print (s1.intersection(s3), 2)
print (s2.intersection(s3), 2)

Output:

{'a', 'b', 'd', 'c'} 3
{'d', 'c', 'a', 'p', 'b'} 2
{'d', 'f', 'c', 'a', 'e', 'b'} 2
{'d', 'c', 'a', 'b', 'q'} 2

Upvotes: 2

Related Questions