【问题标题】:Count non-empty end-leafs of a python dicitonary/array data structure - recursive algorithm?计算python字典/数组数据结构的非空结束叶 - 递归算法?
【发布时间】:2013-08-06 19:34:04
【问题描述】:

我正在寻找一个函数来查找一种复杂字典/数组结构的所有非空端点。我认为因为我不知道嵌套数组的数量或它们的位置,所以它必须是递归的,而且我还没有完全理解这种思维方式。

所以对于嵌套字典:

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}

和变量“paths”命名如下,其中“.XX”表示有一个数组(以variety.js的风格):

vars = ["top.middle.XX.nested",
        "top.last.XX.nested.XX.first",
        "top.last.XX.nested.XX.second",
        "other"]

我想要一个可以返回的函数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

对我来说,问题似乎是尝试构建树,以及在哪里放置空值计数器。所以,我不太明白如何正确记录计数器或进行递归。

【问题讨论】:

  • 应该["first":"","second":""]{"first":"","second":""}
  • 哦,我有几个问题,等一下,让我再修改一下。
  • 下一个问题:如果XX表示一个数组,为什么路径是top.last.XX.nested.XX.first而不是top.last.XX.nested.first?成为top.last.XX.nested 一本字典。
  • 好的,这就是我的意思---谢谢你指出这一点。
  • 听起来你正在做一些接近模式验证的事情。我发现像 voluptuous 这样的工具非常方便。

标签: python recursion map functional-programming nested


【解决方案1】:

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

x = #your x as posted

def byPath (tree, path):
    try: head, tail = path.split ('.', 1)
    except: return tree [path]

    if head == 'XX': return [byPath (node, tail) for node in tree]
    else: return byPath (tree [head], tail)


print (byPath (x, 'top.middle.XX.nested') )
print (byPath (x, 'top.last.XX.nested.XX.first') )
print (byPath (x, 'top.last.XX.nested.XX.second') )
print (byPath (x, 'other') )

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

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) )

【讨论】:

  • 基本上就是这样。谢谢。
猜你喜欢
  • 2019-10-18
  • 1970-01-01
  • 2013-02-09
  • 2011-08-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-09-02
  • 1970-01-01
相关资源
最近更新 更多