简体   繁体   中英

How to create set of sets from dictionary

I need help with a problem in python. i have a python dictionary as shown enter 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)
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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM