How to find which item in a JSON list has the greatest value for a given attribute in Python? -



How to find which item in a JSON list has the greatest value for a given attribute in Python? -

this question has reply here:

in list of dicts, find min() value of mutual dict field 4 answers

i'm using json info sfg worldcup api.

what need find recent goal scored given team in given match. that, need sort value of attribute key in each element of array attribute of away_team_events attribute.

let me illustrate.

here's sample json french republic ongoing (at time of writing) french republic v switzerland.

"away_team_events": [ { "id": 276, "type_of_event": "goal", "player": "giroud", "time": "17" }, { "id": 277, "type_of_event": "goal", "player": "matuidi", "time": "18" }, { "id": 278, "type_of_event": "penalty-wrong", "player": "benzema", "time": "32" }, { "id": 279, "type_of_event": "goal", "player": "valbuena", "time": "40" }, { "id": 281, "type_of_event": "substitution-in", "player": "pogba", "time": "63" }, { "id": 282, "type_of_event": "substitution-in", "player": "koscielny", "time": "66" }, { "id": 283, "type_of_event": "goal", "player": "benzema", "time": "67" } ]

so need here find "id" attribute greatest, because latest goal.

how sort specific attribute this?

i've seen this question, can't create sense of answers.

edit: rephrase, sorry confusion.

i don't need rearrange them, how identify item in list has greatest id, , utilize that?

here solution, utilize max() function. have tell max how sort, in case, id field:

import json open('events.json') f: events = json.load(f) event = max(events['away_team_events'], key=lambda ev: ev['id']) print event

output:

{u'type_of_event': u'goal', u'player': u'benzema', u'id': 283, u'time': u'67'} discussion events json info 1 key: away_team_events events['away_team_events'] list of 7 items. list, going select item greatest event id

python json sorting json-api

Comments

Popular posts from this blog

php - Android app custom user registration and login with cookie using facebook sdk -

django - Access session in user model .save() -

php - .htaccess Multiple Rewrite Rules / Prioritizing -