为了账号安全,请及时绑定邮箱和手机立即绑定

如何将字典中的嵌套值更改为上级键

如何将字典中的嵌套值更改为上级键

拉风的咖菲猫 2021-03-29 13:54:36
我有特定格式的json文件,但我想将其更改为需要拾取其中一个嵌套值并将其投影为上级键的格式我只尝试了stackoverflow的建议,并很累地达到了特定的嵌套值,但是我无法提高它。data = open('sampleDemo.json','r')json_decode=json.load(data)for item in data:    mydict={}    mydict['spk1'] = item.get('audioResponse').get('startTime').get('emotion').get.....    print mydict所需的输出:输入文件如下所示:{   "audioResponse": [  {     "speaker":"spk1",     "startTime": "0.0",     "endTime": "15.239",     "emotion": "neutral",     "emotionScore": "0"  },  {     "speaker":"silence",     "startTime": "15.240",     "endTime": "16.900",     "emotion": "NA",     "emotionScore": "NA"  },  {     "speaker":"spk2",     "startTime": "16.932",     "endTime": "27.320",     "emotion": "happy",     "emotionScore": "2"  },  {     "speaker":"spk1",     "startTime": "27.977",     "endTime": "37.900",     "emotion": "anger",     "emotionScore": "-3"  } ]}
查看完整描述

3 回答

?
富国沪深

TA贡献1790条经验 获得超9个赞

var data = {

  "audioResponse": [{

      "speaker": "spk1",

      "startTime": "0.0",

      "endTime": "15.239",

      "emotion": "neutral",

      "emotionScore": "0"

    }, {

      "speaker": "silence",

      "startTime": "15.240",

      "endTime": "16.900",

      "emotion": "NA",

      "emotionScore": "NA"

    }, {

      "speaker": "spk2",

      "startTime": "16.932",

      "endTime": "27.320",

      "emotion": "happy",

      "emotionScore": "2"

    }, {

      "speaker": "spk1",

      "startTime": "27.977",

      "endTime": "37.900",

      "emotion": "anger",

      "emotionScore": "-3"

    }]

};


var result = {

  audioResponse: []

};


for (var item of data.audioResponse) {

  var existed = result.audioResponse.find(x => x[item.speaker]);

  if (!existed)

    result.audioResponse.push({

      [item.speaker]: []

    });


  existedArray = result.audioResponse.find(x => x[item.speaker])[item.speaker];


  var copy = {...item};

  delete copy.speaker;

  existedArray.push(copy);

}


console.log(result);


查看完整回答
反对 回复 2021-04-08
?
慕雪6442864

TA贡献1812条经验 获得超5个赞

var inputList = {

   "audioResponse": [


  {

     "speaker":"spk1",

     "startTime": "0.0",

     "endTime": "15.239",

     "emotion": "neutral",

     "emotionScore": "0"

  },


  {

     "speaker":"silence",

     "startTime": "15.240",

     "endTime": "16.900",

     "emotion": "NA",

     "emotionScore": "NA"

  },


  {

     "speaker":"spk2",

     "startTime": "16.932",

     "endTime": "27.320",

     "emotion": "happy",

     "emotionScore": "2"

  },


  {

     "speaker":"spk1",

     "startTime": "27.977",

     "endTime": "37.900",

     "emotion": "anger",

     "emotionScore": "-3"

  } 

]}





var result = inputList.audioResponse.reduce((acc, {speaker, ...rest})=>{

    acc['audioResponse'][speaker] = (acc['audioResponse'][speaker]) || [];

    acc['audioResponse'][speaker].push(rest);

    return acc;

}, {audioResponse:{}});


console.log(result);


查看完整回答
反对 回复 2021-04-08
?
长风秋雁

TA贡献1757条经验 获得超7个赞

但我想将其更改为需要拾取其中一个嵌套值并将其投影为上级键的格式


您可以将组形成的属性设置为动态


'use strict';


let attribute = 'speaker'; // can be 'speaker' or 'emotion' or 'emotionScore'

let output = {

  audioResponse: []

};


for (let response of data.audioResponse) {

  let groupKey = response[attribute];

  let group = output.audioResponse[groupKey];

  delete response[attribute]; //remove the key

  if (group) {

    group.push(response);

  } else {

    group = [response];

  }

  output.audioResponse[groupKey] = group;

}


console.log(output);


查看完整回答
反对 回复 2021-04-08
  • 3 回答
  • 0 关注
  • 235 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信