I have a list and I want to made some manipulation on it. This my list :
data_two =
{
'2021-12-05': [['Mesure 1', {
'valeur': 0,
'name': 'User 2'
}, {
'valeur': 22.6,
'name': 'User 1'
}
],['Mesure 2', {
'valeur': 15,
'name': 'User 2'
}, {
'valeur': 6,
'name': 'User 1'
}
]]
}
and I want to have something like this
data_two =
{
'2021-12-05': [
['Mesure 1', {
'valeur': 0,
'name': 'User 2'
}, {
'valeur': 22.6,
'name': 'User 1'
}, 22.6, '7h', ''],
['Mesure 2', {
'valeur': 15,
'name': 'User 2'
}, {
'valeur': 6,
'name': 'User 1'
}, 21, '6h', ''],
['TOTAL', 15, 28.6, '']
]
}
For each items in data_two
, I have to make a sum of valeur
, so 0+22.6=22.6
and 15+6=21
. Then 7h
is the index of the max element. The first index is 6h
The element ['TOTAL', 15, 28.6, '']
, is the just sum of element grouped by index. So you have 0+15=15
and 22.6+6=28.6
I wrote the code bellow for it
for key, values in data_two.items():
valeurs_total = []
total_1 = 0
total_2 = 0
for cval in values:
print(cval)
maximum = 0
h = ''
total_1 += cval[1]['valeur']
if maximum <= cval[1]['valeur'] and cval[1]['valeur'] > 0:
maximum = cval[1]['valeur']
h = '6h'
total_2 += cval[2]['valeur']
if maximum <= cval[2]['valeur'] and cval[2]['valeur'] > 0:
maximum = cval[2]['valeur']
h = '7h'
cval.extend([maximum, h, ""])
maxValue = [total_1, total_2]
max_value = max(maxValue)
max_index = '' if max_value == 0 else str(maxValue.index(max_value) + 6) + ' h'
valeurs_total.extend(['TOTAL', total_1, total_2])
values.append(valeurs_total)
data_two[key] = values
print(data_two)
How can I made this code better ?