Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/305.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
计算python二进制/数组数据结构的非空端叶数-递归算法?_Python_Recursion_Map_Functional Programming_Nested - Fatal编程技术网

计算python二进制/数组数据结构的非空端叶数-递归算法?

计算python二进制/数组数据结构的非空端叶数-递归算法?,python,recursion,map,functional-programming,nested,Python,Recursion,Map,Functional Programming,Nested,我正在寻找一个函数来查找一种复杂字典/数组结构的所有非空端点。我认为,因为我不知道嵌套数组的数量或它们的位置,所以它必须是递归的,我只是还没有完全理解这种想法 因此,对于嵌套的dict: x = {"top": {"middle" : [ {"nested": "value"}, {"nested":"val2"}, {"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}
变量“path”的名称如下,其中“.XX”表示有一个数组(样式为variety.js):

我想要一个函数
f(x,y)
可以返回

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

对我来说,问题似乎是试图在运行时构造树,以及在哪里放置空值计数器。因此,我不太明白如何记录计数器或正确执行递归。

也许这可以引导您朝着正确的方向前进
byPath
收集嵌套的字典项。调用后,您基本上可以将结果列表展平,并检查您的条件是否满足(如
elem!=''
not elem
或其他内容):

编辑:这里是实际计算非空字符串元素的部分:

def count (result):
    if isinstance (result, list):
        total = 0
        positive = 0
        for e in result:
            r = count (e)
            total += r [1]
            positive += r [0]
        return (positive, total)
    else: return (0 if result == '' else 1, 1)

a = byPath (x, 'top.middle.XX.nested')
b = byPath (x, 'top.last.XX.nested.XX.first')
c = byPath (x, 'top.last.XX.nested.XX.second')
d = byPath (x, 'other')

for x in [a, b, c, d]: print (count (x) )
把所有东西放在一起:

def f (tree, path):
    return count (byPath (tree, path) )

for path in ['top.middle.XX.nested', 'top.last.XX.nested.XX.first', 'top.last.XX.nested.XX.second', 'other']:
    print (path, f (x, path) )

也许这可以指引你正确的方向
byPath
收集嵌套的字典项。调用后,您基本上可以将结果列表展平,并检查您的条件是否满足(如
elem!=''
not elem
或其他内容):

编辑:这里是实际计算非空字符串元素的部分:

def count (result):
    if isinstance (result, list):
        total = 0
        positive = 0
        for e in result:
            r = count (e)
            total += r [1]
            positive += r [0]
        return (positive, total)
    else: return (0 if result == '' else 1, 1)

a = byPath (x, 'top.middle.XX.nested')
b = byPath (x, 'top.last.XX.nested.XX.first')
c = byPath (x, 'top.last.XX.nested.XX.second')
d = byPath (x, 'other')

for x in [a, b, c, d]: print (count (x) )
把所有东西放在一起:

def f (tree, path):
    return count (byPath (tree, path) )

for path in ['top.middle.XX.nested', 'top.last.XX.nested.XX.first', 'top.last.XX.nested.XX.second', 'other']:
    print (path, f (x, path) )

也许应该
[“first”:““second”:“]
{“first”:““second”:“}
?哦,我有几个问题,一秒钟,让我再修改一下。下一个问题:如果
XX
表示数组,为什么路径
top.last.XX.nested.XX.first
而不是
top.last.XX.nested.first
?作为
top.last.XX.nested
a dictionary.OK,现在这就是我的意思---谢谢你指出这一点。听起来你在做一些接近模式验证的事情。我发现像
volupluous
这样的工具非常方便。应该
[“第一”:“,“第二”:”]
{“第一”:“,“第二”:”}
?哦,我有几个问题,一秒钟,让我再修改一下。下一个问题:如果
XX
表示数组,为什么路径
top.last.XX.nested.XX.first
而不是
top.last.XX.nested.first
?作为
top.last.XX.nested
a dictionary.OK,现在这就是我的意思---谢谢你指出这一点。听起来你在做一些接近模式验证的事情。我发现像
volupluous
这样的工具非常方便,基本上就是这样。谢谢,基本上就是这样。非常感谢。