[
{
"uid_1c": "c1f5e839-0c1d-11ed-aa44-ac1f6bd30991",
"number": "1",
"amount": 19
},
{
"uid_1c": "c1f5e839-0c1d-11ed-aa44-ac1f6bd30991",
"number": "2",
"amount": 10
},
{
"uid_1c": "7421a94a-4327-11ed-aa45-ac1f6bd30990",
"number": "3",
"amount": 200
}
]
[
{
"uid_1c": "c1f5e839-0c1d-11ed-aa44-ac1f6bd30991",
"number": "1",
"amount": 29
},
{
"uid_1c": "7421a94a-4327-11ed-aa45-ac1f6bd30990",
"number": "2",
"amount": 200
}
]
def unique_with_sum(arr, id_key, sum_key):
unique = {}
for n in arr:
unique.setdefault(n[id_key], { **n, sum_key: 0 })[sum_key] += n[sum_key]
return [*unique.values()]
result = unique_with_sum(arr, 'uid_1c', 'amount')
import json
json_string = """[
{
"uid_1c": "c1f5e839-0c1d-11ed-aa44-ac1f6bd30991",
"number": "1",
"amount": 19
},
{
"uid_1c": "c1f5e839-0c1d-11ed-aa44-ac1f6bd30991",
"number": "2",
"amount": 10
},
{
"uid_1c": "7421a94a-4327-11ed-aa45-ac1f6bd30990",
"number": "3",
"amount": 200
}
]"""
json_object = json.loads(json_string)
# делаем базовый словарь на каждый уникальный uid
base_dict_elem = {el.get('uid_1c'): {'uid_1c': el.get('uid_1c'), 'number': 0, 'amount': 0} for el in json_object}
# итерируемся по списку словарей(json) для заполнения основного словаря
for el in json_object:
uid = el.get('uid_1c')
amount = el.get('amount')
base_dict_elem[uid]['amount'] += amount
# формируем список словарей с уникальными uid и статистикой
result = []
counter = 1
for k, v in base_dict_elem.items():
v['number'] = counter
result.append(v)
counter += 1
print(result)