I have a list of dictionaries like this.
dicts = [
{"key_a": "2022-01-30",
"key_b": "54",
"key_c": "3"},
{"key_a": "2022-01-30",
"key_b": "46",
"key_c": "2"},
{"key_a": "2022-01-04",
"key_b": "32",
"key_c": "4"}
{"key_a": "2022-01-30",
"key_b": "2",
"key_c": "10"}
]
# check same value of key_a
# Sum key_b
# keep the first key_c value from the dictionaries
I want to merge those with same "date". I tried with Panda but I am looking for a more simple way
This is the ouput I am looking for
new_dicts = [
{"key_a": "2022-01-30",
"key_b": "102",
"key_c": "3"},
{"key_a": "2022-01-04",
"key_b": "32",
"key_c": "4"}
]
CodePudding user response:
def get_dicts(dicts):
dates = {d["key_a"] for d in dicts}
for date in dates:
yield {
"key_a": date,
"key_b": str(sum(map(int, d["key_b"] for d in dicts if d["key_a"] == date))),
"key_c": next(d["key_c"] for d in dicts if d["key_a"] == date)
}
print(list(get_dicts(dicts)))
CodePudding user response:
Here's how I would do it
dates = {}
for dict in dicts:
curr_date = dict['key_a']
if curr_date not in dates:
dates[curr_date] = dict
dates[curr_date]['key_b'] = int(dict['key_b']) # convert key_b to int
else:
# add current key_b to running total
dates[curr_date]['key_b'] = int(dict['key_b'])
new_dicts = list(dates.values())
# convert key_b back to string
for dict in new_dicts:
dict['key_b'] = str(dict['key_b'])
CodePudding user response:
An alternative method (handles string):
new_dicts = []
existing_dates = []
for d in dicts:
if d['key_a'] in existing_dates:
for nd in new_dicts:
if nd['key_a'] == d['key_a']:
nd['key_b'] = int(d['key_b'])
else:
existing_dates.append(d['key_a'])
d['key_b'] = int(d['key_b'])
new_dicts.append(d)
for d in new_dicts:
d['key_b'] = str(d['key_b'])
print(new_dicts)
Also, I believe you're missing a comma in the data posted in the answer.
Output:
[{'key_a': '2022-01-30', 'key_b': '102', 'key_c': '3'}, {'key_a': '2022-01-04', 'key_b': '32', 'key_c': '4'}]
