2016-05-22 10 views
-1

Ich habe folgende JSON-DateiSortierung JSON von Attributwert mit Python

{ 
    "modifyDate": 1463899037000, 
    "champions": [ 
     { 
     "id": 40, 
     "stats": { 
      "totalDeathsPerSession": 60, 
      "totalSessionsPlayed": 18, 
      "totalDamageTaken": 246343, 
      "totalQuadraKills": 0, 
      "totalTripleKills": 0, 
      "totalMinionKills": 272, 
      "maxChampionsKilled": 3, 
      "totalDoubleKills": 0, 
      "totalPhysicalDamageDealt": 121345, 
      "totalChampionKills": 11, 
      "totalAssists": 271, 
      "mostChampionKillsPerSession": 3, 
      "totalDamageDealt": 238803, 
      "totalFirstBlood": 0, 
      "totalSessionsLost": 7, 
      "totalSessionsWon": 11, 
      "totalMagicDamageDealt": 113241, 
      "totalGoldEarned": 176088, 
      "totalPentaKills": 0, 
      "totalTurretsKilled": 10, 
      "mostSpellsCast": 0, 
      "maxNumDeaths": 9, 
      "totalUnrealKills": 0 
     } 
     }, 
     { 
     "id": 111, 
     "stats": { 
      "totalDeathsPerSession": 20, 
      "totalSessionsPlayed": 4, 
      "totalDamageTaken": 60371, 
      "totalQuadraKills": 0, 
      "totalTripleKills": 0, 
      "totalMinionKills": 247, 
      "maxChampionsKilled": 3, 
      "totalDoubleKills": 0, 
      "totalPhysicalDamageDealt": 35727, 
      "totalChampionKills": 4, 
      "totalAssists": 35, 
      "mostChampionKillsPerSession": 3, 
      "totalDamageDealt": 190815, 
      "totalFirstBlood": 0, 
      "totalSessionsLost": 2, 
      "totalSessionsWon": 2, 
      "totalMagicDamageDealt": 145353, 
      "totalGoldEarned": 30823, 
      "totalPentaKills": 0, 
      "totalTurretsKilled": 2, 
      "mostSpellsCast": 0, 
      "maxNumDeaths": 7, 
      "totalUnrealKills": 0 
     } 
     }, 
     { 
     "id": 43, 
     "stats": { 
      "totalDeathsPerSession": 103, 
      "totalSessionsPlayed": 24, 
      "totalDamageTaken": 335867, 
      "totalQuadraKills": 0, 
      "totalTripleKills": 0, 
      "totalMinionKills": 828, 
      "maxChampionsKilled": 10, 
      "totalDoubleKills": 2, 
      "totalPhysicalDamageDealt": 170141, 
      "totalChampionKills": 77, 
      "totalAssists": 302, 
      "mostChampionKillsPerSession": 10, 
      "totalDamageDealt": 923985, 
      "totalFirstBlood": 0, 
      "totalSessionsLost": 7, 
      "totalSessionsWon": 17, 
      "totalMagicDamageDealt": 732367, 
      "totalGoldEarned": 242157, 
      "totalPentaKills": 0, 
      "totalTurretsKilled": 12, 
      "mostSpellsCast": 0, 
      "maxNumDeaths": 8, 
      "totalUnrealKills": 0 
     } 
     }, 
     { 
     "id": 117, 
     "stats": { 
      "totalDeathsPerSession": 150, 
      "totalSessionsPlayed": 36, 
      "totalDamageTaken": 494142, 
      "totalQuadraKills": 0, 
      "totalTripleKills": 0, 
      "totalMinionKills": 2017, 
      "maxChampionsKilled": 8, 
      "totalDoubleKills": 5, 
      "totalPhysicalDamageDealt": 297987, 
      "totalChampionKills": 102, 
      "totalAssists": 418, 
      "mostChampionKillsPerSession": 8, 
      "totalDamageDealt": 1905782, 
      "totalFirstBlood": 0, 
      "totalSessionsLost": 13, 
      "totalSessionsWon": 23, 
      "totalMagicDamageDealt": 1577943, 
      "totalGoldEarned": 353798, 
      "totalPentaKills": 0, 
      "totalTurretsKilled": 15, 
      "mostSpellsCast": 0, 
      "maxNumDeaths": 12, 
      "totalUnrealKills": 0 
     } 
     }, 
     { 
     "id": 254, 
     "stats": { 
      "totalDeathsPerSession": 13, 
      "totalSessionsPlayed": 2, 
      "totalDamageTaken": 43839, 
      "totalQuadraKills": 0, 
      "totalTripleKills": 0, 
      "totalMinionKills": 77, 
      "maxChampionsKilled": 8, 
      "totalDoubleKills": 0, 
      "totalPhysicalDamageDealt": 227018, 
      "totalChampionKills": 12, 
      "totalAssists": 8, 
      "mostChampionKillsPerSession": 8, 
      "totalDamageDealt": 247686, 
      "totalFirstBlood": 0, 
      "totalSessionsLost": 1, 
      "totalSessionsWon": 1, 
      "totalMagicDamageDealt": 3920, 
      "totalGoldEarned": 21321, 
      "totalPentaKills": 0, 
      "totalTurretsKilled": 0, 
      "mostSpellsCast": 0, 
      "maxNumDeaths": 9, 
      "totalUnrealKills": 0 
     } 
     } 
    ], 
    "summonerId": 21193669 
} 

und ich möchte die id s der 3 champions bekommen, die am meisten totalSessionsPlayed haben. Um dies zu tun, würde ich zuerst die champions von totalSessionsPlayed sortieren und dann die ersten 3 id s nehmen. Wie kann ich das tun oder gibt es vielleicht einen besseren Weg dies zu tun, anstatt es zuerst zu sortieren?

+1

Anders als mit 'sorted()'? –

+0

können Sie den Code einfügen, den Sie versucht haben, die Werte und Ihre Logik und nicht nur den JSON zu extrahieren? – glls

+1

Anstelle von 'sorted()' können Sie 'heapq.nlargest()' hier verwenden, aber ich sehe keinen Punkt. –

Antwort

1

Wenn ich das Problem richtig zu verstehen, können Sie heapq.nlargest verwenden das Array partially sort:

import json 
import heapq 

dat = json.loads("(your json here)") 
champions = dat['champions'] 
tsp_getter = lambda x: x['stats']['totalSessionsPlayed'] 
largest = heapq.nlargest(3, champions, key = tsp_getter) 
ids = [c['id'] for c in largest] 

Aber wahrscheinlich einfach sorted wird statt nlargest schön spielen (Sie können Ihre Benchmarks tun, es zu überprüfen):

tsp_getter = lambda x: - x['stats']['totalSessionsPlayed'] 
largest = sorted(champions, key = tsp_getter) 
ids = [c['id'] for c in largest[:3]]