I have a few lists
A =['D','KIT','SAP'],
B= ['F','G','LUFT','SAP'],
C= ['I','SAP'],
D= ['SAP','LUF','KIT'],
F= ['SAP','LUF','KIT','HASS']
I passed them in combinations to a dictionary. For example, I joined lists A and B as a key 'AB' in my new dictionary and appended the values from both lists like the list of values to that key 'AB'.
my_dict={"AB":[['D','KIT','SAP'],['F','G','LUFT','SAP']]}
My aim to get the overlap between the lists in multiple combination. For that, I could get with the following one liner.
for k,v in my_dict.items():
print(k,list(set.intersection(*[set(x) for x in v])))
AB ['SAP']
However, the problem here is that when I have more lists and more combinations it is tedious to define manually a dictionary with all possible combinations.
The aim is to achieve all possible combinations of lists. From the above-mentioned example, I have the following combination of lists. In this case that would be 5*5 +1 = 26 combinations. And in the end, I would like to get the overlap for each of the following combinations.
AB, AC, AD, AF
ABC, ABD, ABF
ABCD, ABCDF
ABCDF
BC, BD, BF
DF, DC
FC
The question is how could I achieve that in a pythonic way. Any suggestions are much appreciated.
As a side note, I do not want a dictionary in between if this combination of overlap is possible from the lists itself.
Thanks
Thanks for all solutions. Each solution was helpful. However, none of them was the exact answer I was looking for. Nonetheless, all of them partly helped me to come up with the right solution. Posting for everyone might be helpful.
I first needed to create 26 combinations for 5 lists. The initial lists
A =['D','KIT','SAP']
B= ['F','G','LUFT','SAP']
C= ['I','SAP']
D= ['SAP','LUF','KIT']
E= ['SAP','LUF','KIT','HASS']
Made a list of lists
lists = [A, B, C, D, E]
Defined empty list called combination as @marco's solution. And added all possible combinations based on the length of lists, plus one. The combinations are the values from our final dictionary.
combinations =[]
for l in range(2, len(lists) + 1):
combinations += itertools.combinations(lists, l)
After that define a dictionary with keys and values from the list of the list. From here I adopted partly @butterflyknife's solution.
master_dict = dict(A=A, B=B, C=C, D=D, F=F)
For the keys of our final dictionary, we defined an empty list first. Then populated the combinations by using join.
key_list = []
for i in range(2,len(master_dict) +1):
for combo in it.combinations(master_dict.keys(), i):
new_key = "".join(combo)
if (len(combo)>1):
key_list.append(new_key)
Now, with both keys and values. We populate our final dictionary.
final_dict = dict()
for key, elem in zip(key_list, combinations):
final_dict[key] = list(elem)
This part is to find the overlaps and convert them to a data frame.
overlap_list = []
dfs = []
for k,v in final_dict.items():
overlap = list(set.intersection(*[set(x) for x in v]))
overlap_list.append(overlap)
#print(overlap, sep=", ")
dct ={ 'overlap':overlap,'number of overlapps':len(overlap),
'overlap models':k}
dfs.append(dct)
df = pd.DataFrame.from_dict(dfs)