Siddhesh Sharma
Siddhesh Sharma

Reputation: 21

Find all the path from root to leaf in dictionary

I am stuck in a problem where i have nested dictionary,

abc = {'transaction': '452899', 'fwd': [{'transaction': '480928', 'fwd': [{'transaction': '1056150', 'fwd': [{'transaction': '1253555', 'fwd': [{'transaction': '2852520'}]}, {'transaction': '1382834', 'fwd': [{'transaction': '1621677'}]}]}]}, {'transaction': '480929'}, {'transaction': '481066'}, {'transaction': '1716590'}]
 
**the output i require is ,**
**nested_list**
[
['452899','480928','1056150','1253555','2852520']
['452899','480928','1056150','1382834','1621677']
['452899','480929']
['452899','481066']
['452899','1716590']
]

i am stuck in it since one day,

my current try

def generate_paths(fwd,answer):
    
    root_trxn = fwd['transaction']
    answer.append(root_trxn)
    if 'fwd' in fwd:
        new_f = fwd['fwd']
        for item in new_f:
            generate_paths(item,answer)

    return(answer)
            
a={'transaction': '452899', 'fwd': [{'transaction': '480928', 'fwd': [{'transaction': '1056150', 'fwd': [{'transaction': '1253555', 'fwd': [{'transaction': '2852520'}]}, {'transaction': '1382834', 'fwd': [{'transaction': '1621677'}]}]}]}, {'transaction': '480929'}, {'transaction': '481066'}, {'transaction': '1716590'}]}
k = generate_paths(a,[])
print(k)

results to wrong output as specified below--->

['452899', '480928', '1056150', '1253555', '2852520', '1382834', '1621677', '480929', '481066', '1716590']

Upvotes: 0

Views: 74

Answers (3)

Killerz0ne
Killerz0ne

Reputation: 344

This should fix your problem. now the function returns the list you wanted:

def generate_paths2(fwd,answer,list_ans):
    
    root_trxn = fwd['transaction']
    answer.append(root_trxn)
    if 'fwd' in fwd:
        new_f = fwd['fwd']
        for item in new_f:
            generate_paths2(item,answer,list_ans)
    else:
        list_ans.append(list(answer))

    answer.pop()
    return(list_ans)
        

Upvotes: 1

Siddhesh Sharma
Siddhesh Sharma

Reputation: 21

i was waiting for answer, but i have found the soltion.

def generate_paths(fwd,answer):
    
    root_trxn = fwd['transaction']
    answer.append(root_trxn)
    if 'fwd' in fwd:
        new_f = fwd['fwd']
        for item in new_f:
            generate_paths(item,answer)
    else:
        print(list(answer))

    answer.pop()
    return(answer)
            
a={'transaction': '452899', 'fwd': [{'transaction': '480928', 'fwd': [{'transaction': '1056150', 'fwd': [{'transaction': '1253555', 'fwd': [{'transaction': '2852520'}]}, {'transaction': '1382834', 'fwd': [{'transaction': '1621677'}]}]}]}, {'transaction': '480929'}, {'transaction': '481066'}, {'transaction': '1716590'}]}

k = generate_paths(a,[])

Upvotes: 0

Minek Po1
Minek Po1

Reputation: 162

I hope this solves your problem:

def generate_paths(t):
    out = [t['transaction']]
    if 'fwd' in t:
        for i in t['fwd']:
            #[1,2,3] + [4,5,6] ==> [1,2,3,4,5,6]
            out.append(generate_paths(i))
    return out

Upvotes: 0

Related Questions