Debug255
Debug255

Reputation: 335

Python Dictionary of Parent Child Map

I am having a difficult time building a dictionary of parent child flags.

I have a dictionary like this:

d = {
'A': ['Blue'],
'B': ['A'],
'C': ['A'],
'D': ['C'],
}

This is my logic or thought process: If a key is 'Blue', it is the parent, and gets a flag of 0. If the value is in d.keys(), it gets a flag of 1. Where I get stuck is on the grandchildren. Here is the code I have now, and is a result of me banging my head against a figurative wall for many hours now.

level = 0
while level < 1000:
    for key, value in d.items():
        if value[0] == 'Set':
            if len(value) == 1:
                value.extend([level])
        elif len(value) >= 2:
            continue
        elif value[0] in d.keys():
            value.extend([level])
        level += 1

The result is:

A:      ['Blue', 0]
B:      ['A', 1]
C:      ['A', 2]
D:      ['D', 3]

D is a grandchild of C and should have a flag of 2, and C is a child of A and should have a value of 2, just like the other child, B.

The purpose of this is to create the correct flags no matter how many grandchildren or great-grandchildren levels there are.

The bigger picture here is to use this data to insert nested loops. I have a strange database and have to use a certain proprietary module to iterate through the lookup tables that are made available to me via class attributes. So, the code will be

while parent:
    ...block of code for parent...
    ...insert same structure here if not the parent... 
    ...statement at end of parent block...

So, a child loop may look like this:

while parent:
    ...block of code for parent...
    while child:
       ...block of code for child...
       ...variable for block insertion...
       ...statement at end of child block...
    ...statement at end of parent block...

The ...statement at end of parent/child block... is a similar to next() but using the proprietary module I need to use for this.

So, the mapper is to be used to let the program know how many nested while loops to create using this structure. I haven't gotten to inserting the nested blocks but I know it can be done. :)

I would love to learn from someone who can explain how create the mapper portion.

Thank you!

Upvotes: 1

Views: 1376

Answers (3)

PM 2Ring
PM 2Ring

Reputation: 55479

This code modifies your existing child: parent dictionary, adding the depth flags to the end of each list. It will work correctly (both in Python 2 & Python 3) no matter what order the keys are processed in. In versions before Python 3.6, dictionaries don't necessarily preserve the insertion order of the keys, and the key order can change from one run of the program to the next. So I scrambled the entries a little to make sure that it does behave as expected.

src = {
    'D': ['C'],
    'B': ['A'],
    'A': ['Blue'],
    'C': ['A'],
}

def get_depth(d, k):
    v = d[k]
    if len(v) == 2:
        return v[1]
    else:
        parent = v[0]
        depth = 0 if parent == 'Blue' else get_depth(d, parent) + 1
        v.append(depth)
        return depth

for k in src:
    get_depth(src, k)

print(src)    

Python 3.6 output

{'D': ['C', 2], 'B': ['A', 1], 'A': ['Blue', 0], 'C': ['A', 1]}

Upvotes: 1

fl00r
fl00r

Reputation: 83680

d = {
  'A': ['Blue'],
  'B': ['A'],
  'C': ['A'],
  'D': ['C'],
}

output = {}
for k, v in d.items():
    output[v[0]] = output.get(v[0], [])
    output[v[0]].append(k)

output
#=> {'Blue': ['A'], 'A': ['C', 'B'], 'C': ['D']}

Upvotes: 1

cuzi
cuzi

Reputation: 1087

I am not sure I completely understand what the expected output should be, but this might help anyway

for key, value in d.items():
    if value[0] == 'Set':
        if len(value) == 1:
            value.extend([0])
    elif len(value) >= 2:
        continue
    elif value[0] in d.keys():
        newlevel = d[value[0]][1] + 1
        value.extend([newlevel])

Upvotes: 0

Related Questions