qq_38360687
2021-08-09 15:35
采纳率: 50%
浏览 88
已结题

关于python中将多个数据生成一个嵌套字典

__

data = {"nodeDataArray": [
    {"category": "Start", "text": "start", "key": -1},
    {"category": "Logic", "text": "and", "key": -2},
    {"category": "Logic", "text": "and", "key": -3},
    {"category": "Operator", "text": "==", "key": -4},
    {"category": "Operator", "text": "==", "key": -5},
    {"category": "Logic", "text": "and", "key": -6},
    {"category": "Operator", "text": ">=", "key": -7},
    {"category": "Operator", "text": "<=", "key": -8},
    {"category": "Variable", "text": "age", "key": -9},
    {"category": "Variable", "text": "age", "key": -10},
    {"category": "Constant", "text": "18", "key": -11},
    {"category": "Constant", "text": "50", "key": -12},
    {"category": "Variable", "text": "salary", "key": -13},
    {"category": "Constant", "text": "5000", "key": -14},
    {"category": "Variable", "text": "rent", "key": -15},
    {"category": "Constant", "text": "1000", "key": -16}
],
    "linkDataArray": [
        {"from": -1, "to": -2},
        {"from": -2, "to": -3},
        {"from": -2, "to": -4},
        {"from": -3, "to": -6},
        {"from": -3, "to": -5},
        {"from": -6, "to": -7},
        {"from": -6, "to": -8},
        {"from": -7, "to": -9},
        {"from": -7, "to": -11},
        {"from": -8, "to": -10},
        {"from": -8, "to": -12},
        {"from": -5, "to": -13},
        {"from": -5, "to": -14},
        {"from": -4, "to": -15},
        {"from": -4, "to": -16}
    ]}

将上面数据转换成下面这种样子
上面数据中key表示当前节点,from为to的前面一个节点。上面的key和下面的to是对应的

img

{'and': [{'and': [{'and': [{'>=': [{'var': 'age'}, 18]}, {'<=': [{'var': 'age'}, 50]}]}, {'==': [{'var': 'salary'}, 5000]}]},{'==': [{'var': 'rent'}, 1000]}]}
  • 收藏

1条回答 默认 最新

  • 已采纳

    你题目的解答代码如下:(如有帮助,望采纳!谢谢! 点击我这个回答右上方的【采纳】按钮)

    data = {"nodeDataArray": [
        {"category": "Start", "text": "start", "key": -1},
        {"category": "Logic", "text": "and", "key": -2},
        {"category": "Logic", "text": "and", "key": -3},
        {"category": "Operator", "text": "==", "key": -4},
        {"category": "Operator", "text": "==", "key": -5},
        {"category": "Logic", "text": "and", "key": -6},
        {"category": "Operator", "text": ">=", "key": -7},
        {"category": "Operator", "text": "<=", "key": -8},
        {"category": "Variable", "text": "age", "key": -9},
        {"category": "Variable", "text": "age", "key": -10},
        {"category": "Constant", "text": "18", "key": -11},
        {"category": "Constant", "text": "50", "key": -12},
        {"category": "Variable", "text": "salary", "key": -13},
        {"category": "Constant", "text": "5000", "key": -14},
        {"category": "Variable", "text": "rent", "key": -15},
        {"category": "Constant", "text": "1000", "key": -16}
    ],
        "linkDataArray": [
            {"from": -1, "to": -2},
            {"from": -2, "to": -3},
            {"from": -2, "to": -4},
            {"from": -3, "to": -6},
            {"from": -3, "to": -5},
            {"from": -6, "to": -7},
            {"from": -6, "to": -8},
            {"from": -7, "to": -9},
            {"from": -7, "to": -11},
            {"from": -8, "to": -10},
            {"from": -8, "to": -12},
            {"from": -5, "to": -13},
            {"from": -5, "to": -14},
            {"from": -4, "to": -15},
            {"from": -4, "to": -16}
        ]}
    
    
    dicdata = {x["key"]:x for x in data["nodeDataArray"]}
    
    def jnp(s):
        if dicdata[s]["category"] == "Variable":
            return {'var': dicdata[s]["text"]}
        elif dicdata[s]["category"] == "Constant":
            return int(dicdata[s]["text"])
        else:
            li = [x["to"] for x in data["linkDataArray"] if x["from"]==s]
            tli = []
            for v in li:
                tli.append(jnp(v))
            dic = {dicdata[s]["text"]: tli}
            return dic
    
    start = list(filter(lambda x: x["category"]=="Start", data["nodeDataArray"]))[0]["key"]
    res = jnp(start)
    print(res)
    

    结果:

    {'start': [{'and': [{'and': [{'and': [{'>=': [{'var': 'age'}, 18]}, {'<=': [{'var': 'age'}, 50]}]}, {'==': [{'var': 'salary'}, 5000]}]}, {'==': [{'var': 'rent'}, 1000]}]}]}
    
    1 打赏 评论

相关推荐 更多相似问题