Home > OS >  How to create set of sets from dictionary
How to create set of sets from dictionary

Time:11-18

I need help with a problem in python. i have a python dictionary as shownenter image description here. I want to create a set of sets from the dictionary such that each set covers all the keys. for instance, i want an output like this:enter image description here. we see that each set has exactly one element from each key of the dictionary. I am still new to programming, and I recently started learning python. below is what I have tried so far Thanks This is the pseudo-code I am trying to reproduce, but I have not been able to make any progress because it has several confusing lines. enter image description here

Rplus[i] = {'i1': {'r1', 'r3', 'r7'},'i2': {'r10', 'r8'},'i3': {'r4', 'r5', 'r9'},'i4': {'r2', 'r6'}}

S = [{'r1', 'r10', 'r5','r2'}, {'r3', 'r8', 'r4', 'r6'}, {'r2', 'r5', 'r8', 'r1'},......., {'r10', 'r6', 'r4', 'r7'}]

S = []
Sprime = []

for i in items: 
    if len(Rplus[i])==1:
        if len(S)==0:
            S.append(Rplus[i])
        else:
            for s in range(len(S)):
                S[s].union(Rplus[i])
    else:
        Sprime = copy.deepcopy(S)
        for r in Rplus[i]:
            if len(Sprime) == 0:
                Sprime.append({r})
            else:
                for j in range(len(Sprime)):
                    Sprime[j].update([r])
                    if Sprime[j] not in S:
                        S.append(Sprime[j])             
print(S)

CodePudding user response:

import itertools

set_of_tuples=itertools.product(*original_dict.values())
list_of_sets = [set(x) for x in bb]

set_of_tuples contains all of the possible combinations of one value from each key. list_of_sets convert to output format you have a picture of.

  • Related