i have list structure look like this :
example =
[
{
"value":"promo",
"score":0.3333333333333333,
"slugger":"promoKeyword",
"type":"normal",
},
{
"value":"unknown",
"score":1.0,
"slugger":"promoCategory",
"type":"normal",
},
{
"value":"theory",
"score":0.3333333333333333,
"slugger":"promoCategory",
"type":"normal",
},
{
"value":"theory",
"score":0.5,
"slugger":"promoCart",
"type":"normal",
}
]
i want to filter the list by maximum score in [score] key if only the [slugger] key has same value(this mean [slugger] can have multiple same value and we only take the highest score of it)
so the example will look like this
[
{
"value":"promo",
"score":0.3333333333333333,
"slugger":"promoKeyword",
"type":"normal",
},
{
"value":"unknown",
"score":1.0,
"slugger":"promoCategory",
"type":"normal",
},
{
"value":"theory",
"score":0.5,
"slugger":"promoCart",
"type":"normal",
}
]
my effort right now look like this,but it fails to satisfied the condition
score_data = []
for data in example:
score_data.append(data['score'])
max_score = max(score_data)
example = [x for x in example if x['score'] == max_score and x['score'] > 0]
example = list({ each['slug'] : each for each in example }.values())
can you guys help ? thank you in advance..pardon my english
groupby- sort bysluggervalue, then group by it (groupbyonly groups adjacent elements, hence the sorting first), and then you can take the max.