Python之数据结构改造(一)

博客提出将列表数据改造成全字典格式的需求,目的是让日志更易解析。思路为定义新字典,以唯一的id作为键更新新字典,更新完成后删掉部分原数据。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

{
  "appList":[ 
{
    "id" : 120,
    "name" : "BIGDATA",
    "alias" : "大数据",
    "appInsList" : [ 
 {
      "id" : 590,
      "host_name" : "10.131.34.17-uds1",
      "alias" : null,
      "server_port" : 111111
    }, {
      "id" : 592,
      "host_name" : "10.131.34.17-uds1",
      "alias" : null,
      "server_port" : 111111
    }, {
      "id" : 594,
      "host_name" : "10.131.34.17-uds1",
      "alias" : null,
      "server_port" : 111111
    }, {
      "id" : 610,
      "host_name" : "10.131.30.239-sh-dsjfjh-app1",
      "alias" : null,
      "server_port" : 0
    }, {
      "id" : 618,
      "host_name" : "10.131.30.239-sh-dsjfjh-app1",
      "alias" : null,
      "server_port" : 111111
    }, {
      "id" : 620,
      "host_name" : "10.131.34.17-uds1",
      "alias" : null,
      "server_port" : 111111
    }, {
      "id" : 626,
      "host_name" : "10.131.34.17-uds1",
      "alias" : null,
      "server_port" : 111111
    }, {
      "id" : 634,
      "host_name" : "10.131.34.18-uds2",
      "alias" : null,
      "server_port" : 111111
    }, {
      "id" : 636,
      "host_name" : "10.131.34.18-uds2",
      "alias" : null,
      "server_port" : 111111
    }, {
      "id" : 638,
      "host_name" : "10.131.34.18-uds2",
      "alias" : null,
      "server_port" : 11111
    }, {
      "id" : 640,
      "host_name" : "10.131.34.17-uds1",
      "alias" : null,
      "server_port" : 11111
    }, {
      "id" : 726,
      "host_name" : "10.131.34.12-bigdata12.bigdata.com",
      "alias" : null,
      "server_port" : 0
    }, {
      "id" : 728,
      "host_name" : "10.131.34.12-bigdata12.bigdata.com",
      "alias" : null,
      "server_port" : 11111
    }, {
      "id" : 730,
      "host_name" : "10.131.34.13-bigdata13.bigdata.com",
      "alias" : null,
      "server_port" : 0
    }, {
      "id" : 732,
      "host_name" : "10.131.34.13-bigdata13.bigdata.com",
      "alias" : null,
      "server_port" : 111111
    }, {
      "id" : 734,
      "host_name" : "10.131.34.13-bigdata13.bigdata.com",
      "alias" : null,
      "server_port" : 111111
    }, {
      "id" : 736,
      "host_name" : "10.131.34.12-bigdata12.bigdata.com",
      "alias" : null,
      "server_port" : 111111
    } ]
  }, {
    "id" : 124,
    "name" : "SHUJUFUWU",
    "alias" : "数据管理",
    "appInsList" : [ {
      "id" : 612,
      "host_name" : "10.131.25.110-SH-sjgl-app1",
      "alias" : null,
      "server_port" : 111111
    }, {
      "id" : 614,
      "host_name" : "10.131.25.118-SH-sjgl-app2",
      "alias" : null,
      "server_port" : 0
    }, {
      "id" : 616,
      "host_name" : "10.131.25.118-SH-sjgl-app2",
      "alias" : null,
      "server_port" : 11111
    } ]
  }, {
    "id" : 126,
    "name" : "YDHL",
    "alias" : "移动互联",
    "appInsList" : [ {
      "id" : 622,
      "host_name" : "10.131.30.203-ydhlyy-app03",
      "alias" : null,
      "server_port" : 111111
    }, {
      "id" : 628,
      "host_name" : "10.131.30.204-ydhlyy-app04",
      "alias" : null,
      "server_port" : 111111
    }, {
      "id" : 630,
      "host_name" : "10.131.30.202-ydhlyy-app02",
      "alias" : null,
      "server_port" : 111111
    }, {
      "id" : 632,
      "host_name" : "10.131.30.201-ydhlyy-app01",
      "alias" : null,
      "server_port" : 111111
    }, {
      "id" : 716,
      "host_name" : "10.131.30.201-ydhlyy-app01",
      "alias" : null,
      "server_port" : 11111
    }, {
      "id" : 718,
      "host_name" : "10.131.30.201-ydhlyy-app01",
      "alias" : null,
      "server_port" : 11111
    }, {
      "id" : 720,
      "host_name" : "10.131.30.202-ydhlyy-app02",
      "alias" : null,
      "server_port" : 11111
    }, {
      "id" : 722,
      "host_name" : "10.131.30.202-ydhlyy-app02",
      "alias" : null,
      "server_port" : 11111
    } ]
  }, {
    "id" : 128,
    "name" : "ythjf2.0",
    "alias" : "营销缴费",
    "appInsList" : [ {
      "id" : 648,
      "host_name" : "10.131.38.6-SH-ythjf-app4",
      "alias" : null,
      "server_port" : 111111
    }, {
      "id" : 650,
      "host_name" : "10.131.38.6-SH-ythjf-app4",
      "alias" : null,
      "server_port" : 0
    }, {
      "id" : 652,
      "host_name" : "10.131.38.6-SH-ythjf-app4",
      "alias" : null,
      "server_port" : 111111
    }, {
      "id" : 654,
      "host_name" : "10.131.38.5-SH-ythjf-app3",
      "alias" : null,
      "server_port" : 11111
    }, {
      "id" : 656,
      "host_name" : "10.131.38.5-SH-ythjf-app3",
      "alias" : null,
      "server_port" : 0
    }, {
      "id" : 658,
      "host_name" : "10.131.38.6-SH-ythjf-app4",
      "alias" : null,
      "server_port" : 111111
    }, {
      "id" : 660,
      "host_name" : "10.131.38.5-SH-ythjf-app3",
      "alias" : null,
      "server_port" : 11111
    }, {
      "id" : 662,
      "host_name" : "10.131.38.5-SH-ythjf-app3",
      "alias" : null,
      "server_port" : 111111
    }, {
      "id" : 790,
      "host_name" : "10.131.38.6-SH-ythjf-app4",
      "alias" : null,
      "server_port" : 111111
    }, {
      "id" : 792,
      "host_name" : "10.131.38.5-SH-ythjf-app3",
      "alias" : null,
      "server_port" : 111111
    } ]
  }, {
    "id" : 130,
    "name" : "xmhxt",
    "alias" : "新企业门户",
    "appInsList" : [ {
      "id" : 664,
      "host_name" : "10.131.34.187-SH-nwqybgmh-app2",
      "alias" : null,
      "server_port" : 111111
    }, {
      "id" : 666,
      "host_name" : "10.131.34.187-SH-nwqybgmh-app2",
      "alias" : null,
      "server_port" : 111111
    }, {
      "id" : 676,
      "host_name" : "10.131.34.189-SH-nwqybgmh-app4",
      "alias" : null,
      "server_port" : 111111
    }, {
      "id" : 678,
      "host_name" : "10.131.34.189-SH-nwqybgmh-app4",
      "alias" : null,
      "server_port" : 11111111
    }, {
      "id" : 680,
      "host_name" : "10.131.34.188-SH-nwqybgmh-app3",
      "alias" : null,
      "server_port" : 11111
    }, {
      "id" : 682,
      "host_name" : "10.131.34.188-SH-nwqybgmh-app3",
      "alias" : null,
      "server_port" : 11111
    }, {
      "id" : 788,
      "host_name" : "10.131.34.189-SH-nwqybgmh-app4",
      "alias" : null,
      "server_port" : 0
    } ]
  }, {
    "id" : 136,
    "name" : "ZHSJPT",
    "alias" : "综合审计",
    "appInsList" : [ {
      "id" : 738,
      "host_name" : "10.131.32.205-vm-zhsj-app2",
      "alias" : null,
      "server_port" : 11111
    }, {
      "id" : 740,
      "host_name" : "10.131.32.205-vm-zhsj-app2",
      "alias" : null,
      "server_port" : 777777
    }, {
      "id" : 742,
      "host_name" : "10.131.32.204-vm-zhsj-app1",
      "alias" : null,
      "server_port" : 6666
    }, {
      "id" : 744,
      "host_name" : "10.131.32.204-vm-zhsj-app1",
      "alias" : null,
      "server_port" : 22222
    }, {
      "id" : 912,
      "host_name" : "10.131.32.204-vm-zhsj-app1",
      "alias" : null,
      "server_port" : 0
    }, {
      "id" : 914,
      "host_name" : "10.131.32.205-vm-zhsj-app2",
      "alias" : null,
      "server_port" : 0
    } ]
  }, {
    "id" : 144,
    "name" : "PMS20_NEWTEST",
    "alias" : null,
    "appInsList" : [ {
      "id" : 894,
      "host_name" : "10.130.49.44-PMSTESTAPP4",
      "alias" : null,
      "server_port" : 3333
    } ]
  }, {
    "id" : 146,
    "name" : "TY_test_0125_",
    "alias" : null,
    "appInsList" : [ {
      "id" : 896,
      "host_name" : "10.131.41.17-localhost",
      "alias" : null,
      "server_port" : 0
    }, {
      "id" : 898,
      "host_name" : "10.131.41.17-localhost",
      "alias" : null,
      "server_port" : 3333
    } ]
  }, {
    "id" : 148,
    "name" : "PMS20_TEST7003",
    "alias" : null,
    "appInsList" : [ {
      "id" : 900,
      "host_name" : "10.130.49.44-PMSTESTAPP4",
      "alias" : null,
      "server_port" : 0
    }, {
      "id" : 902,
      "host_name" : "10.130.49.44-PMSTESTAPP4",
      "alias" : null,
      "server_port" : 3333
    } ]
  }]
}



appdict = {}

for i in ret['appList']:
    appinsdict = {}
    appdict.update({i.get('id'):i})
    for j in i['appInsList']:
        appinsdict.update({j.get('id'):j})

    appdict.update({i.get('id'):appinsdict})

del ret['appList']
ret.update(appdict)
print(ret)


需求:不需要列表数据,改造成全是字典格式的,方便日志易解析
思路:定义新字典,更新新字典,新字典的键是id,因为id是唯一的,更新完新字典后删掉部分原数据

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值