简体   繁体   English

我如何像Python GROUP BY一样基于python dict中的某些键组合值

[英]How can I combine values based on some key in python dict just like SQL GROUP BY

L = [{'id':1, 'quantity':1}, {'id':2, 'quantity':2}, {'id':1, 'quantity':3}]

I want to add quantity base on id , 我想基于id添加数量

So for the list above I would like the output to be: 因此,对于上面的列表,我希望输出为:

 [{'id':1,'quantity':4},{'id':2,'quantity':2}]

another example: 另一个例子:

L = [{'id':1, 'quantity':1}, {'id':2, 'quantity':2}, {'id':1, 'quantity':2}, {'id':1, 'quantity':3}]

So for the list above I would like the output to be: 因此,对于上面的列表,我希望输出为:

 [{'id':1, 'quantity':6}, {'id':2, 'quantity':2}]

In python "group by" functionality may be achieved by itertools.groupby() function: python中, “分组依据”功能可以通过itertools.groupby()函数实现:

import itertools

l = [{'id':1, 'quantity':1}, {'id':2, 'quantity':2}, {'id':1, 'quantity':3}]
result = [ {'id': k, 'quantity': sum(_['quantity'] for _ in g)} 
            for k,g in itertools.groupby(sorted(l, key=lambda x:x['id']), key=lambda x:x['id'])]

print(result)

The output: 输出:

[{'id': 1, 'quantity': 4}, {'id': 2, 'quantity': 2}]

This should do what you want: 这应该做您想要的:

from collections import defaultdict

def combine(items):
    counts = defaultdict(int)
    for d in items:
        counts[d["id"]] += d["quantity"]

    return [{"id": id, "quantity": q} for id, q in counts.items()]

Examples: 例子:

>>> combine([{'id':1, 'quantity':1}, {'id':2, 'quantity':2}, {'id':1, 'quantity':3}])
[{'quantity': 4, 'id': 1}, {'quantity': 2, 'id': 2}]

>>> combine([{'id':1, 'quantity':1}, {'id':2, 'quantity':2}, {'id':1, 'quantity':2}, {'id':1, 'quantity':3}])
[{'quantity': 6, 'id': 1}, {'quantity': 2, 'id': 2}]

This is about as simple and efficient as you're going to get. 这与您将要获得的一样简单和高效。

convert it to dataframe and then back to dict 将其转换为dataframe ,然后返回到dict

import pandas as pd
L = [{'id':1, 'quantity':1}, {'id':2, 'quantity':2}, {'id':1, 'quantity':3}]
output=pd.DataFrame(L).groupby('id')['quantity'].sum().to_dict()

Assuming the input is properly defined, here I implemented in a intuitive way to achieve this: 假设输入定义正确,这里我以一种直观的方式实现了这一点:

output = {}
keys=[]
for e in L:
    if e['id'] not in keys:
        keys.append(e['id'])
        output[e['id']] = e['quantity']
    else:
        output[e['id']] += e['quantity']

[{'id':key,'identity':values} for key,values in  output.items()]

I was actually wondering that is there any further requirements, for instance, that you need a probably higher efficiency to perform a huge volume of data? 我实际上在想是否还有其他要求,例如,您需要更高的效率来执行大量数据? If yes, this method seems to be tedious. 如果是,则此方法似乎很乏味。

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

相关问题 如何在Python中根据Key对Json数据进行分组并合并值? - How to Group Json data based on Key and combine values under in Python? Python:基于一个特定键相同的所有字典键值组合 - Python: Combine all dict key values based on one particular key being the same 如何在Python的字典中检查值是否具有相同/相同的键? - how to check values if it has a like/same key in a dict in python? 如何根据其中一个关键值将此连接查询的结果转换为dict? - How can I turn the results from this joined query into dict based on one of the key values? 如何根据密钥结尾的内容拆分 dict - How can I split dict based on what the key ends with 如何按 Python 中的多键字典一键分组? - How to group by a multikey dict in Python by one key? 如何在基于字符串值的复杂树状嵌套dict中搜索键值对,该字符串值在Python中包含类似于Jinja的分隔符 - How to search for key value pairs in a complex tree-like nested dict based on string values which contains a Jinja-like separators in Python 根据属性列表值选择性地组合字典的 Python 列表 - Combine Python List of Dict's optionally based on properties lists values 如何基于定界符区分dict键和值 - How to differentiate the dict key and values based on delimiters 如何仅检查特定 dict 键的唯一值? - how can I checking only a spefic dict key for unique values?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM