如何解决获取Python中JSON密钥的平均和
我对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","rank": 3205,"outOf": 5055,"duration": 67599,"startTime": 1597001988440,"fightID": 11,"percentile": 36.597235868212486,"total": 130.623,{
"encounterID": 667,"encounterName": "Shazzrah","rank": 2655,"outOf": 12663,"duration": 27960,"startTime": 1597003660411,"fightID": 32,"percentile": 79.02778951070532,"total": 441.345,]
这是我正在使用的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)
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。