suppose i have been given a list of dictionaries like this :
[{"id":1, "symbol":'A', "num":4}, {"id":2, "symbol":'A', "num":3}, {"id":1, "symbol":'A', "num":5}, {"id":2, "symbol":'B', "num":1}]
Now, i have to create a dictionary or alter the current one such that (id, symbol) together are unique and the num value is the sum of all the values present in the dict with that (id,symbol) so that the new dict or the current dict looks something like this:
[{"id":1, "symbol":'A', "num":9}, {"id":2, "symbol":'A', "num":3}, {"id":2, "symbol":'B', "num":1}]
答案 0 :(得分:0)
I would go the pandas.DataFrame
way -
import pandas as pd
a = [{"id":1, "symbol":'A', "num":4}, {"id":2, "symbol":'A', "num":3}, {"id":1, "symbol":'A', "num":5}, {"id":2, "symbol":'B', "num":1}]
b = pd.DataFrame(a)
c = b.groupby(['id', 'symbol'])['num'].sum().reset_index()
print(c)
d = list(c.to_dict(orient='index').values())
print(d)
Output
[{'id': 1, 'symbol': 'A', 'num': 9}, {'id': 2, 'symbol': 'A', 'num': 3}, {'id': 2, 'symbol': 'B', 'num': 1}]
Hope that works!
答案 1 :(得分:0)
You can use groupby
in the following manner:
from itertools import groupby
from operator import itemgetter
grouper = itemgetter("id", "symbol")
result = []
for key, grp in groupby(sorted(input_data, key = grouper), grouper):
temp_dict = dict(zip(["id", "symbol"], key))
temp_dict["num"] = sum(item["num"] for item in grp)
result.append(temp_dict)
from pprint import pprint
pprint(result)
Output:
[{'id': 1, 'num': 9, 'symbol': 'A'},
{'id': 2, 'num': 3, 'symbol': 'A'},
{'id': 2, 'num': 1, 'symbol': 'B'}]
答案 2 :(得分:0)
Here's a solution with plain python.
dlst = [{"id":1, "symbol":'A', "num":4}, {"id":2, "symbol":'A', "num":3},
{"id":1, "symbol":'A', "num":5}, {"id":2, "symbol":'B', "num":1}]
# Create a dict where keys are tuples of (id,symbol), values are num
combined_d = {}
for d in dlst:
id_sym = (d["id"], d["symbol"])
if id_sym in combined_d:
combined_d[id_sym] += d["num"]
else:
combined_d[id_sym] = d["num"]
# create a list of dictionaries from the tuple-keyed dict
result = []
for k, v in combined_d.items():
d = {"id": k[0], "symbol": k[1], "num": v}
result.append(d)
print(result)
It does what you want, but the resulting list is not sorted, as it's built from a dictionary.