python中的数据转换

时间:2019-10-18 07:43:52

标签: python-3.x

我下面的数据存储在变量“ json_ls”中

print(json_ls)

[{'status': 'Take', 'batch_number': '1001a', 'recipient_id': '5517a5'}, 
{'status': 'Take', 'batch_number': '1001b', 'recipient_id': '5517a6'},
{'status': 'Take', 'batch_number': '1001c', 'recipient_id': '5517a7'}, 
{'status': 'Take', 'batch_number': '1001d', 'recipient_id': '5517a9'}, 
{'status': 'Take', 'batch_number': '1001a', 'recipient_id': '5517aa'}, 
{'status': 'Take', 'batch_number': '1001a', 'recipient_id': '5517ab'}, 
{'status': 'Take', 'batch_number': '1001b', 'recipient_id': '5517ac'}, 
{'status': 'Take', 'batch_number': '1001b', 'recipient_id': '5517ad'},
{'status': 'Take', 'batch_number': '1001c', 'recipient_id': '5517ae'},
{'status': 'Take', 'batch_number': '1001c', 'recipient_id': '5517af'},
{'status': 'Take', 'batch_number': '1001d', 'recipient_id': '5517b0'},
{'status': 'Take', 'batch_number': '1001d', 'recipient_id': '5517b1'},
{'status': 'Take', 'batch_number': '1001a', 'recipient_id': '5517b2'},
{'status': 'Take', 'batch_number': '1001a', 'recipient_id': '5517b3'}, 
{'status': 'Take', 'batch_number': '1001a', 'recipient_id': '5517b4'}]

我需要转换以上数据并获得如下所示的结果。我想将每个批次的数据分组,并在数组中保留所有各自的receive_id,状态。

[
{'batch_number': '1001a', 
'recipients': 
[{'recipient_id': '5517a5', 'status': 'Take'},
{'recipient_id': '5517aa', 'status': 'Take'},
{'recipient_id': '5517ab', 'status': 'Take'},
{'recipient_id': '5517b2', 'status': 'Take'},
{'recipient_id': '5517b3', 'status': 'Take'},
{'recipient_id': '5517b4', 'status': 'Take'}]}, 

{'batch_number': '1001b', 
'recipients': 
[{'recipient_id': '5517a6', 'status': 'Take'},
{'recipient_id': '5517ac', 'status': 'Take'},
{'recipient_id': '5517ad', 'status': 'Take'}]}, 

{'batch_number': '1001c', 
'recipients': 
[{'recipient_id': '5517a7', 'status': 'Take'},
{'recipient_id': '5517ae', 'status': 'Take'},
{'recipient_id': '5517af', 'status': 'Take'}]}, 

{'batch_number': '1001d', 
'recipients': 
[{'recipient_id': '5517a9', 'status': 'Take'},
{'recipient_id': '5517b0', 'status': 'Take'},
{'recipient_id': '5517b1', 'status': 'Take'}]}
]

我有大约5K记录要转换,并正在寻找有效的转换方法。有人可以帮我吗?

1 个答案:

答案 0 :(得分:0)

使用collections.defaultdict

例如:

from collections import defaultdict

json_ls = [{'status': 'Take', 'batch_number': '1001a', 'recipient_id': '5517a5'}, 
{'status': 'Take', 'batch_number': '1001b', 'recipient_id': '5517a6'},
{'status': 'Take', 'batch_number': '1001c', 'recipient_id': '5517a7'}, 
{'status': 'Take', 'batch_number': '1001d', 'recipient_id': '5517a9'}, 
{'status': 'Take', 'batch_number': '1001a', 'recipient_id': '5517aa'}, 
{'status': 'Take', 'batch_number': '1001a', 'recipient_id': '5517ab'}, 
{'status': 'Take', 'batch_number': '1001b', 'recipient_id': '5517ac'}, 
{'status': 'Take', 'batch_number': '1001b', 'recipient_id': '5517ad'},
{'status': 'Take', 'batch_number': '1001c', 'recipient_id': '5517ae'},
{'status': 'Take', 'batch_number': '1001c', 'recipient_id': '5517af'},
{'status': 'Take', 'batch_number': '1001d', 'recipient_id': '5517b0'},
{'status': 'Take', 'batch_number': '1001d', 'recipient_id': '5517b1'},
{'status': 'Take', 'batch_number': '1001a', 'recipient_id': '5517b2'},
{'status': 'Take', 'batch_number': '1001a', 'recipient_id': '5517b3'}, 
{'status': 'Take', 'batch_number': '1001a', 'recipient_id': '5517b4'}]

result = defaultdict(list)
for i in json_ls:
    result[i.pop("batch_number")].append(i)
result = [{'batch_number': k, 'recipients':v}  for k, v in result.items()]    
print(result)

输出:

[{'batch_number': '1001a',
  'recipients': [{'recipient_id': '5517a5', 'status': 'Take'},
                 {'recipient_id': '5517aa', 'status': 'Take'},
                 {'recipient_id': '5517ab', 'status': 'Take'},
                 {'recipient_id': '5517b2', 'status': 'Take'},
                 {'recipient_id': '5517b3', 'status': 'Take'},
                 {'recipient_id': '5517b4', 'status': 'Take'}]},
 {'batch_number': '1001b',
  'recipients': [{'recipient_id': '5517a6', 'status': 'Take'},
                 {'recipient_id': '5517ac', 'status': 'Take'},
                 {'recipient_id': '5517ad', 'status': 'Take'}]},
 {'batch_number': '1001c',
  'recipients': [{'recipient_id': '5517a7', 'status': 'Take'},
                 {'recipient_id': '5517ae', 'status': 'Take'},
                 {'recipient_id': '5517af', 'status': 'Take'}]},
 {'batch_number': '1001d',
  'recipients': [{'recipient_id': '5517a9', 'status': 'Take'},
                 {'recipient_id': '5517b0', 'status': 'Take'},
                 {'recipient_id': '5517b1', 'status': 'Take'}]}]