简体   繁体   English

使用python3.6中的多个键在dict内分组列表

[英]Group list inside dict using multiple keys in python3.6

Here I'm given input JSON data.在这里,我得到了输入 JSON 数据。 Anyone help me to solve expected result below format.任何人都可以帮助我解决以下格式的预期结果。 I'm trying to solve but I can't get expected result format.我正在尝试解决,但我无法获得预期的结果格式。

sub_type_list = [
   {
    'name':'blood',
    'transaction_points':[
        {
            'point':'(1-10)',
            'value':'',
            'symbol':'',
            'service_id':''
        },
        {
            'point':'(10-20)',
            'value':'',
            'symbol':'',
            'service_id': "423"
        },
        {
            'point':'(20-30)',
            'value':'',
            'symbol':'',
            'service_id':'1'
        }
    ]
  },
  {
    'name':'blood',
    'transaction_points':[
        {
            'point':'(1-10)',
            'value':'',
            'symbol':'',
            'service_id':'123'
        },
        {
            'point':'(10-20)',
            'value':'',
            'symbol':'',
            'service_id':''
        },
        {
            'point':'(20-30)',
            'value':'',
            'symbol':'',
            'service_id':''
        }       
    ]           
  },
  {
    'name':'body',
    'transaction_points':[
        {
            'point':'(1-10)',
            'value':'',
            'symbol':'',
            'service_id':''
        },
        {
            'point':'(10-20)',
            'value':'',
            'symbol':'',
            'service_id':'42'
        },
        {
            'point':'(20-30)',
            'value':'',
            'symbol':'',
            'service_id':'11'
        }
    ]
  },
  {
    'name':'blood',
    'transaction_points':[
        {
            'point':'(1-10)',
            'value':'',
            'symbol':'',
            'service_id':'87'
        },
        {
            'point':'(10-20)',
            'value':'',
            'symbol':'',
            'service_id':'50'
        },
        {
            'point':'(20-30)',
            'value':'',
            'symbol':'',
            'service_id':'25'
        }
    ]
  }
 ]

Expected Result below预期结果如下

 [{
'name': 'blood',
'transaction_points':[
  [
      { 'point': '(1-10)', 'value': '', 'symbol': '', 'service_id': ''},
      {'point': '(1-10)', 'value': '', 'symbol': '', 'service_id': '123'},
      { 'point': '(1-10)', 'value': '', 'symbol': '', 'service_id': '87'}       
],
[     
     {'point': '(10-20)', 'value': '', 'symbol': '', 'service_id': '423'},
     {'point': '(10-20)', 'value': '', 'symbol': '', 'service_id': ''},
     {'point': '(10-20)', 'value': '', 'symbol': '', 'service_id': '50'}    
],
 [
     {'point': '(20-30)', 'value': '', 'symbol': '', 'service_id': '1'},
     '{point': '(20-30)', 'value': '', 'symbol': '', 'service_id': ''},
     '{point': '(20-30)', 'value': '', 'symbol': '', 'service_id': '25'}
    
 ]
]
},{
'name': 'body',
'transaction_points':[
  [
     {'point': '(1-10)', 'value': '', 'symbol': '', 'service_id': ''}
    
],
[ 
    {'point': '(10-20)', 'value': '', 'symbol': '', 'service_id': '42'} 
],
 [
    {'point': '(20-30)', 'value': '', 'symbol': '', 'service_id': '11'}     
]
]
}
]

Here I'm using given below code and try to generate expected result在这里,我使用下面给出的代码并尝试生成预期的结果

 result = dict()
 final_result = []
 for id, i in enumerate(sub_type_list):
     if result.get(i["name"]):
          result[i["name"]].extend(i["transaction_points"])
     else:
          result[i["name"]] = i["transaction_points"]
 for i in result.keys():
      final_result.append({"name": i, "transaction_points": result[i]})

final_result list produce give below result but I want expected result above final_result 列表产生下面的结果,但我想要上面的预期结果

 [
 {
    'name':'blood',
    'transaction_points':[
        {
            'point':'(1-10)',
            'value':'',
            'symbol':'',
            'service_id':''
        },
        {
            'point':'(10-20)',
            'value':'',
            'symbol':'',
            'service_id':'423'
        },
        {
            'point':'(20-30)',
            'value':'',
            'symbol':'',
            'service_id':'1'
        },
        {
            'point':'(1-10)',
            'value':'',
            'symbol':'',
            'service_id':'123'
        },
        {
            'point':'(10-20)',
            'value':'',
            'symbol':'',
            'service_id':''
        },
        {
            'point':'(20-30)',
            'value':'',
            'symbol':'',
            'service_id':''
        },
        {
            'point':'(1-10)',
            'value':'',
            'symbol':'',
            'service_id':'87'
        },
        {
            'point':'(10-20)',
            'value':'',
            'symbol':'',
            'service_id':'50'
        },
        {
            'point':'(20-30)',
            'value':'',
            'symbol':'',
            'service_id':'25'
        }
    ]
},
{
    'name':'body',
    'transaction_points':[
        {
            'point':'(1-10)',
            'value':'',
            'symbol':'',
            'service_id':''
        },
        {
            'point':'(10-20)',
            'value':'',
            'symbol':'',
            'service_id':'42'
        },
        {
            'point':'(20-30)',
            'value':'',
            'symbol':'',
            'service_id':'11'
        }
      ]
    }
   ]

You need to have a look at groupby() function of itertools library.您需要查看 itertools 库的groupby() function。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM