通过在变更列表上迭代来填充数据结构,只有最后一次更新出现在输出中

2024-09-28 19:19:51 发布

您现在位置:Python中文网/ 问答频道 /正文

我花了一天的大部分时间想弄明白这一点。我主要是一名perl开发人员,所以我在python中苦苦挣扎。我不明白为什么只有树的最后一片叶子和所有内部节点都有数据,而所有其他叶子都是空的

import json

data = [{'pos': '0-all.0', 'fld': 'labeled_element', 'ncmp': 'iexact', 'val': 'C'}, {'pos': '0-all.1', 'fld': 'labeled_element', 'ncmp': 'iexact', 'val': 'D'}, {'pos': '0-all.2-any.0', 'fld': 'labeled_element', 'ncmp': 'iexact', 'val': 'E'}, {'pos': '0-all.2-any.1', 'fld': 'labeled_element', 'ncmp': 'iexact', 'val': 'F'}]

def formsetToStructure(formset):
    qry = []
    for form in formset:
        curqry = qry

        # Get the path for this leaf
        path = form["pos"].split(".")

        # For each "edge"/"branch" of the path
        for spot in path:

            # inner nodes have a dash, e.g. "0-all" is the root inner node of type "all"
            pos_gtype = spot.split("-")
            if len(pos_gtype) == 2:
                pos = pos_gtype[0]
                gtype = pos_gtype[1]
            else:
                pos = spot
                gtype = None
            pos = int(pos)

            # Populate the node with a number of branches
            while len(curqry) <= pos:
                curqry.append({})

            # If this is an inner node, populate the inner node
            if gtype is not None:
                # This is a group
                curqry[pos]["pos"] = ""
                curqry[pos]["type"] = "group"
                curqry[pos]["val"] = gtype
                curqry[pos]["queryGroup"] = []
                curqry = curqry[pos]["queryGroup"]
                print("Setting pointer to",pos,"queryGroup")

            # Populate the leaf
            else:
                # This is a field
                print("Setting pos",pos,"type to field")
                curqry[pos]["type"] = "field"
                for key in form.keys():
                    if key == "pos":
                        curqry[pos]["pos"] = ""
                    elif key not in curqry:
                        print("Setting pos",pos,key,"to",form[key])
                        curqry[pos][key] = form[key]
                print()
    return qry

qry = formsetToStructure(data)
print(json.dumps(qry, indent=4))

我要把这个说出来:

Setting pointer to 0 queryGroup
Setting pos 0 type to  field
Setting pos 0 fld to labeled_element
Setting pos 0 ncmp to iexact
Setting pos 0 val to C

Setting pointer to 0 queryGroup
Setting pos 1 type to  field
Setting pos 1 fld to labeled_element
Setting pos 1 ncmp to iexact
Setting pos 1 val to D

Setting pointer to 0 queryGroup
Setting pointer to 2 queryGroup
Setting pos 0 type to  field
Setting pos 0 fld to labeled_element
Setting pos 0 ncmp to iexact
Setting pos 0 val to E

Setting pointer to 0 queryGroup
Setting pointer to 2 queryGroup
Setting pos 1 type to  field
Setting pos 1 fld to labeled_element
Setting pos 1 ncmp to iexact
Setting pos 1 val to F
[
    {
        "pos": "",
        "type": "group",
        "val": "all",
        "queryGroup": [
            {},
            {},
            {
                "pos": "",
                "type": "group",
                "val": "any",
                "queryGroup": [
                    {},
                    {
                        "type": "field",
                        "pos": "",
                        "fld": "labeled_element",
                        "ncmp": "iexact",
                        "val": "F"
                    }
                ]
            }
        ]
    }
]

这是我希望得到的json输出:

[
    {
        "pos": "",
        "type": "group",
        "val": "all",
        "queryGroup": [
            {
                "type": "field",
                "pos": "",
                "fld": "labeled_element",
                "ncmp": "iexact",
                "val": "C"
            },
            {
                "type": "field",
                "pos": "",
                "fld": "labeled_element",
                "ncmp": "iexact",
                "val": "D"
            },
            {
                "pos": "",
                "type": "group",
                "val": "any",
                "queryGroup": [
                    {
                        "type": "field",
                        "pos": "",
                        "fld": "labeled_element",
                        "ncmp": "iexact",
                        "val": "E"
                    },
                    {
                        "type": "field",
                        "pos": "",
                        "fld": "labeled_element",
                        "ncmp": "iexact",
                        "val": "F"
                    }
                ]
            }
        ]
    }
]

我是瞎子还是在某种程度上误解了python

给你一个想法这是做什么/它的目的是什么。。。它正在处理此高级搜索页面的输入:

HTML page allowing data entry of nested query conditions


Tags: toposfieldtypevalelementsettinglabeled
1条回答
网友
1楼 · 发布于 2024-09-28 19:19:51

问题是,您要从根开始,在每次迭代中重新初始化数据结构,删除以前填写的数据

更改:

            if gtype is not None:
                curqry[pos]["pos"] = ""
                curqry[pos]["type"] = "group"
                curqry[pos]["val"] = gtype
                curqry[pos]["queryGroup"] = []

…到

            if gtype is not None:
                if not curqry[pos]: # only initialize if empty
                    curqry[pos]["pos"] = ""
                    curqry[pos]["type"] = "group"
                    curqry[pos]["val"] = gtype
                    curqry[pos]["queryGroup"] = []

相关问题 更多 >