简体   繁体   中英

Get average sum of JSON Keys in Python

I am pretty new to Python and JSON. I am building a tool that lets me quickly analyze a list of players.

I am trying to parse the following JSON and get the average sum of the "percentile" key from all entries.

[   
    {
    "encounterID": 663,
    "encounterName": "Lucifron",
    "class": "Priest",
    "spec": "Healer",
    "rank": 1471,
    "outOf": 4463,
    "duration": 48171,
    "startTime": 1597001801681,
    "reportID": "74LJdPNCZahwmxyW",
    "fightID": 9,
    "difficulty": 3,
    "characterID": 42962953,
    "characterName": "Insanepriest",
    "server": "Lakeshire",
    "percentile": 67.03123111833183,
    "ilvlKeyOrPatch": 70,
    "azeritePowers": [],
    "total": 225.509,
    "estimated": true
  },
  {
    "encounterID": 664,
    "encounterName": "Magmadar",
    "class": "Priest",
    "spec": "Healer",
    "rank": 3205,
    "outOf": 5055,
    "duration": 67599,
    "startTime": 1597001988440,
    "reportID": "74LJdPNCZahwmxyW",
    "fightID": 11,
    "difficulty": 3,
    "characterID": 42962953,
    "characterName": "Insanepriest",
    "server": "Lakeshire",
    "percentile": 36.597235868212486,
    "ilvlKeyOrPatch": 70,
    "azeritePowers": [],
    "total": 130.623,
    "estimated": true
  },
  {
    "encounterID": 667,
    "encounterName": "Shazzrah",
    "class": "Priest",
    "spec": "Healer",
    "rank": 2655,
    "outOf": 12663,
    "duration": 27960,
    "startTime": 1597003660411,
    "reportID": "74LJdPNCZahwmxyW",
    "fightID": 32,
    "difficulty": 3,
    "characterID": 42962953,
    "characterName": "Insanepriest",
    "server": "Lakeshire",
    "percentile": 79.02778951070532,
    "ilvlKeyOrPatch": 70,
    "azeritePowers": [],
    "total": 441.345,
    "estimated": true
  },
]

This is the python code I am using, I only managed to sum all of the items.

response = requests.get('https://classic.warcraftlogs.com/v1/parses/character/Insanepriest/Lakeshire/EU?metric=dps&zone=1000&partition=2&api_key=6436362d91e87781841b4b031f7c0a6c')
    response.raise_for_status()
    # access JSOn content
    jsonResponse = response.json()
    parse = round(jsonResponse[0]["percentile"], 1)
    sum = 0
    for parse in jsonResponse:
        if parse and "percentile" in parse.keys():
            sum += parse["percentile"]
        print(sum)

Any help would be much appreciated. I am sorry if I am asking a dumb question here but I couldn't find anything online.

Best Regards

percentiles = [round(i['percentile'], 1) for i in jsonResponse]
percentilesSum = sum(percentiles)
#I dont know what you mean by average sum, I count it as average
percentilesAvg = percentilesSum / len(percentiles)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM