I am really a newbie in python. Could you please help in the following problem
I have the following table:
Parent Child value 1 Value 2
a0 b1 0.2 5
b5 c1 0.3 2
a0 b2 0.5 6
a1 b3 0.6 3
a1 b4 0.7 58
a2 b5 0.5 63
f0 a0 0.6 9
f0 a1 0.8 5
f0 a2 0.5 5
a0 b0 0.2 6
b5 c0 0.6 4
I need to do recursive parent child relationship and need to find all parents with a given child. However, I also need to multiply the values with each other for a give parent and child combination.
Expected output
Root Parent Child Path Value 3
f0 b5 c1 f0/a2/b5/c1 0.5 * 0.5 * 0.3 (f0->a2 * a2->b5 * b5->c1)
f0 a0 b0 f0/a0/b0 0.6 * 0.2 (f0->a0 * a0->b0)
f0 a1 b4 f0/a1/b4 ..and so on
f0 a1 b3 f0/a1/b3
f0 b5 c0 f0/a2/b5/c0
f0 a0 b2 f0/a0/b2
f0 a0 b1 f0/a0/b1
I am using the following code which works perfectly fine. I got this code from this site only. However, I am not able to figure out how to get the value 3 by multiplying the value 1 as a recourse through the path.
parents = set() children = {} for c,p in ancestry:
parents.add(p)
children[c] = p
def ancestors(p):
return (ancestors(children[p]) if p in children else []) + [p]
for k in (set(children.keys()) - parents): print ('/'.join(ancestors(k)))
any help is greatly appreciated.