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

关于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]}]}]}
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

问题事件

  • 系统已结题 8月18日
  • 已采纳回答 8月10日
  • 修改了问题 8月9日
  • 创建了问题 8月9日

悬赏问题

  • ¥15 逻辑谓词和消解原理的运用
  • ¥15 三菱伺服电机按启动按钮有使能但不动作
  • ¥15 js,页面2返回页面1时定位进入的设备
  • ¥200 关于#c++#的问题,请各位专家解答!网站的邀请码
  • ¥50 导入文件到网吧的电脑并且在重启之后不会被恢复
  • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。
  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝
  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号