[英]how to combine data in Python Dictionary
I have a python dictionary like 我有一个像python的字典
my_list = [{'orderproduct_id': 76, 'orderproduct_quantity': '500', 'orderbatch_id': 'FOX19022019-2'},
{'orderproduct_id': 76, 'orderproduct_quantity': '500', 'orderbatch_id': 'FOX19022019-1'},
{'orderproduct_id': 77, 'orderproduct_quantity': '100', 'orderbatch_id': 'FOX19022019-1'}]
I want output like 我想要输出像
my_list = [{'orderproduct_id': 76,
'batches': [{'orderproduct_quantity': '500', 'orderbatch_id': 'FOX19022019-2'},
{'orderproduct_quantity': '500', 'orderbatch_id': 'FOX19022019-1'}
]
},
{'orderproduct_id': 77,
'batches': [{'orderproduct_quantity': '100', 'orderbatch_id': 'FOX19022019-1'}
]
}]
I am using that in Django view so if any Django function or python concept can work please let me know. 我在Django视图中使用了它,因此,如果任何Django函数或python概念可以工作,请告诉我。 Thanks.
谢谢。
Try something like the following! 尝试以下类似的方法! First of all, create a dictionary to keep as key the
orderproduct_id
, appending every orderproduct_quantity
and orderbatch_id
(both as a dictionary to a list). 首先,创建一个字典以将
orderproduct_id
保留为关键字, orderproduct_id
每个orderproduct_quantity
和orderbatch_id
都追加到列表中(两者都作为字典)。 Then, just convert the dictionary to a list of dictionaries using a compression list, where the key will be the orderproduct_id
and the value , the batches
. 然后,只是转换字典中使用压缩列表,其中的关键将是一个字典列表
orderproduct_id
和值 ,该batches
。
output = {}
for p in my_list:
id = p['orderproduct_id']
if id not in output: # Create empty list for new IDs
output[id] = []
d = { 'orderproduct_quantity': p['orderproduct_quantity'],
'orderbatch_id': p['orderbatch_id'] }
output[id].append(d)
output = [{ 'orderproduct_id': k, 'batches': v } for k, v in output.items()]
You can use itertools: 您可以使用itertools:
from itertools import groupby
new_list = [ {'orderproduct_id': k, 'batches': list(i)}
for k, i in groupby(my_list, lambda x: x['orderproduct_id']) ]
Edited 编辑
Edited to: 编辑为:
groupby
safely. groupby
。 New code: 新代码:
from itertools import groupby
my_key = lambda x: x['orderproduct_id']
[ {'orderproduct_id': k,
'batches': [ d for d in i if d.pop('orderproduct_id') ]}
for k, i in groupby( sorted(my_list,key=my_key), my_key ) ]
Explanation 说明
You are grouping by orderproduct_id
. 您正在按
orderproduct_id
分组。 Then, the natural way is to use groupby
function. 然后,自然的方法是使用
groupby
函数。 When another developer see groupby
in few seconds he/she understand that the code is grouping elements. 当另一位开发人员在几秒钟内看到
groupby
,他/她就知道代码正在对元素进行分组。
Also, my approach uses list comprehension because you are creating new list/dictionaries from another iterables. 另外,我的方法使用列表理解,因为您是从另一个可迭代对象创建新的列表/字典。
You can aggregate your items by orderproduct_id
is separated dict: 您可以按
orderproduct_id
分隔dict来汇总商品:
aggregator = defaultdict(list)
for item in my_list:
aggregator[item.pop('orderproduct_id')].append(item)
Then make a new list out of aggregated dict items: 然后根据聚合的字典项创建一个新列表:
new_list = []
for key, value in aggregator.items():
new_list.append({
'orderproduct_id': key,
'batches': value
})
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.