简体   繁体   中英

merge python dictionary of sets

I have a graph with 2 kinds of nodes- 'Letter nodes' (L) and 'Number nodes' (N). I have 2 dictionaries, one shows edges from L to N and the other shows edges from N to L.

 A = {0:(b,), 1:(c,), 2:(c,), 3:(c,)}
 B = {a:(3,), b:(0,), c:(1,2,3)} 

A key,value pair c:(1,2,3) means there are edges from c to 1,2,3 (3 edges)

I want to merge these to one dictionary C so that the result is a new dictionary:

C = {(0,): (b,), (1, 2, 3): (a, c)}

or

C = {(b,):(0,), (a, c):(1, 2, 3)}

In the resulting dictionary I want the letter nodes and numerical nodes to be on separate sides of keys and values. I don't care which is the key or value just need them separated. How can I go about solving this efficiently?

CLARIFICATION: this of a graph with 2 types of nodes - number nodes, and letter nodes. the dictionary C says from letter nodes (a,c) you can reach the number nodes (1,2,3) ie a->3->c->1, a->3->c->2 thus you can get to 1,2,3 from a. EVEN THOUGH THERE IS NO DIRECT EDGE FROM a to 2 or a to 1.

According to your statement, I guess you are trying to find a graph algorithms.

import itertools
def update_dict(A, result): #update vaules to the same set
    for k in A:
        result[k] = result.get(k, {k}).union(set(A[k]))
        tmp = None
        for i in result[k]:
            tmp = result.get(k, {k}).union(result.get(i, {i}))
        result[k] = tmp
        for i in result[k]:
            result[i] = result.get(i, {i}).union(result.get(k, {k}))

A = {0:('b',), 1:('c',), 2:('c',), 3:('c',)}
B = {'a':(3,), 'b':(0,), 'c':(1,2,3)}
result = dict()
update_dict(A, result)
update_dict(B, result)
update_dict(A, result) #update to fix bugs
update_dict(B, result)

k = sorted([sorted(list(v)) for v in result.values()]) 
k = list( k for k, _ in itertools.groupby(k))  #sort and remove dumplicated set

final_result = dict()
for v in k: #merge the result as expected
    final_result.update({tuple([i for i in v if isinstance(i, int)]):tuple([i for i in v if not isinstance(i, int)])})
print final_result

#output
{(0,): ('b',), (1, 2, 3): ('a', 'c')}

So I'm not sure if this is the most efficient way of doing this at this point, but it works:

 A = {0:('b',), 1:('c',), 2:('c',), 3:('c',)}
 B = {'a':(3,), 'b':(0,), 'c':(1,2,3)} 

# Put B in the same form as A

B_inv = {}
for k, v in B.items():
    for i in v:
        if B_inv.get(i) is not None:
            B_inv[i] = B_inv[i].union(k)
        else:
            B_inv[i] = set(k)

B_inv = {k: tuple(v) for k, v in B_inv.items()}
AB = set(B_inv.items() + A.items())  # get AB as merged

This gets you the merged dictionaries. From here:

new_dict = {}
for a in AB:
    for i in a[1]:
        if new_dict.get(i) is not None:
            new_dict[i] = new_dict[i].union([a[0]])
        else:
            new_dict[i] = set([a[0]])

# put in tuple form
new_dict = {tuple(k): tuple(v) for k,v in new_dict.items()}

This gives me:

{('a',): (3,), ('b',): (0,), ('c',): (1, 2, 3)}

Basically, I'm relying on the mutability of sets and their built-in functionality of eliminating duplicates to try to keep the number of loops through each dictionary to a minimum. Unless I missed something, this should be in linear time.

From here, I need to do comparison, and relying on sets again to prevent me from needing to do a worst-case pairwise comparison of every single element.

merge_list = []

for k, v in new_dict.items():
    matched = False
    nodeset = set([k[0]]).union(v)
    for i in range(len(merge_list)):
        if len(nodeset.intersection(merge_list[i])) != 0:
            merge_list[i] = merge_list[i].union(nodeset)
            matched = True

    # did not find shared edges
    if not matched:
        merge_list.append(nodeset)

Finally, turn it into the form with a single "layer" and tuples.

C = {}

for item in merge_list:
    temp_key = []
    temp_val = []

    for i in item:
        if str(i).isalpha():
            temp_key.append(i)
        else:
            temp_val.append(i)

    C[tuple(temp_key)] = tuple(temp_val)

C gives me {('a', 'c'): (1, 3, 2), ('b',): (0,)} .

try this:

c = a.copy()
c.update(b)

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