Trevor Ferree
Trevor Ferree

Reputation: 45

How do I get every permutation of a dictionary possibilies from two lists?

I have multiple sets of two lists that I need to convert into one dictionary by looking at every permutation across rows in a dataframe.

For example, if there is a list of ['cat1','cat2'] and a list of ['top1','top2'], I'd like a resulting dictionary of {'cat1':'top1','cat1':'top2','cat2':'top1','cat2':'top2'}

Here is my current code that gets close but ends up using every letter and not string...

import pandas as pd

test_df = pd.DataFrame()
test_df['category'] = [['cat1'],['cat2'],['cat3','cat3.5'],['cat5']]
test_df['topic'] = [['top1'],[''],['top2','top3'],['top4']]


final_dict = {}
res = {}

for index, row in test_df.iterrows():
    print(row["category"], row["topic"])
    temp_keys = row["category"]
    temp_values = row["topic"]
    res = {}
    for test_key in temp_keys:
        #print(test_key)
        for test_value in temp_values:
            #print(test_value)
            #print(res)
            test_key = str(test_key)
            print(test_key)
            test_value = str(test_value)
            print(test_value)
            #res[key] = key
            #res = dict(zip(str(key),str(test_value)))
            res = dict(zip(str(test_key),str(test_value)))
            print(res)
            print('\n')

Upvotes: 0

Views: 95

Answers (1)

Corralien
Corralien

Reputation: 120559

If you want a list of tuple instead of dict, you can use pd.MultiIndex.from_product:

out = test_df.apply(pd.MultiIndex.from_product, axis=1).apply(list)
>>> out
0                                       [(cat1, top1)]
1                                           [(cat2, )]
2    [(cat3, top2), (cat3, top3), (cat3.5, top2), (...
3                                       [(cat5, top4)]
dtype: object

>>> out.tolist()
[[('cat1', 'top1')],
 [('cat2', '')],
 [('cat3', 'top2'), ('cat3', 'top3'), ('cat3.5', 'top2'), ('cat3.5', 'top3')],
 [('cat5', 'top4')]]

Upvotes: 1

Related Questions