I'm looking for a function to find all the non-empty end-points of a kind of complex dictionary/array structure. I think that because I don't know the number of nested arrays or their locations, it would have to be recursive, and I just don't entirely get that way of thinking yet.
So for the nested dict:
x = {"top": {"middle" : [
{"nested": "value"},
{"nested":"val2"},
{"nested":""}
],
"last" : [
{"nested": [
{"first":1,"second":1},
{"first":0,"second":""}
]
},
{"nested": [
{"first":1,"second":1},
{"first":1,"second":2}
]
},
{"nested": [
{"first":1,"second":1},
{"first":"","second":""}
]
}
]
},
"other":1}
and variable "paths" named as follows, where ".XX" designates there is an array (in the style of variety.js):
vars = ["top.middle.XX.nested",
"top.last.XX.nested.XX.first",
"top.last.XX.nested.XX.second",
"other"]
I'd like a function f(x,y)
that can return...
f(x,"top.middle.XX.nested") = 2/3
f(x,"top.last.XX.nested.XX.first") = 5/6
f(x,"top.last.XX.nested.XX.second") = 4/6
f(x,"other") = 1
The problem for me seems to be trying to construct the tree as you go and where to put the counter for nulls. So, I don't quite understand how to record the counters or do recursion correctly.