简体   繁体   中英

Group list inside dict using multiple keys in python3.6

Here I'm given input JSON data. 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

 [
 {
    '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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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