获取 Python 中 JSON 个键的平均总和
Get average sum of JSON Keys in Python
我对 Python 和 JSON 很陌生。我正在构建一个工具,可以让我快速分析玩家列表。
我正在尝试解析以下 JSON 并从所有条目中获取“百分位数”键的平均总和。
[
{
"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
},
]
这是我正在使用的 python 代码,我只设法对所有项目求和。
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)
如有任何帮助,我们将不胜感激。很抱歉,如果我在这里问了一个愚蠢的问题,但我在网上找不到任何东西。
此致
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)
我对 Python 和 JSON 很陌生。我正在构建一个工具,可以让我快速分析玩家列表。
我正在尝试解析以下 JSON 并从所有条目中获取“百分位数”键的平均总和。
[
{
"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
},
]
这是我正在使用的 python 代码,我只设法对所有项目求和。
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)
如有任何帮助,我们将不胜感激。很抱歉,如果我在这里问了一个愚蠢的问题,但我在网上找不到任何东西。
此致
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)