2016-11-08 7 views
0
import urllib.request as request 

import json 

api = "https://kr.api.pvp.net/championmastery/location/KR/player/38281748/topchampions?api_key=RGAPI-6bdee369-a91d-485a-9280-444de0e37afe" 

api_data = request.urlopen(api).read().decode("utf-8") 

apiload = json.loads(api_data) 

print(apiload) 

私はLeague of Legendsチャンピオンのポイントを印刷したいと思います。リストの辞書をPython 3の辞書に変換するにはどうしたらいいですか?

だから私はこのAPI https://developer.riotgames.com/api/methods#!/1091/3768を使用し、

は、Pythonオブジェクトに変換します。このAPIの戻り値はList [ChampionMasteryDTO]、

です。つまり、辞書として使用することはできません。

apiloadが含まれている[{ "キー": "値"}、... { "キー": "値"}]

は、どのように私は辞書としてapiloadを作ることができますか?

答えて

0

apiload可変印刷2つの辞書:collections.Counterを使用します。

あなたは次の操作を行うことができますapiloadを使用して新しい辞書を作成したい場合:

championId : 91 tokensEarned : 0 championPointsSinceLastLevel : 339079 chestGranted : True lastPlayTime : 1478451844000 playerId : 38281748 championLevel : 7 championPoints : 360679 championPointsUntilNextLevel : 0 championId : 5 tokensEarned : 0 championPointsSinceLastLevel : 129110 chestGranted : True lastPlayTime : 1478454752000 playerId : 38281748 championLevel : 7 championPoints : 150710 championPointsUntilNextLevel : 0 championId : 21 tokensEarned : 0 championPointsSinceLastLevel : 2018 chestGranted : False lastPlayTime : 1476197348000 playerId : 38281748 championLevel : 4 championPoints : 14618 championPointsUntilNextLevel : 6982

希望:

#create a new dictionary 
my_dict = {} 
#now iterate through the list 
for item in apiload: 
#now iterate through the dictionaries that are in the list: 
    for key, value in item.items(): 
#assign the key value to the new declared dictionary 
    new_dict[key] = value 

これは、次のような出力を持つ新しい辞書を作成します。それは助ける。

+1

これはまさに私が欲しかったものです。あなたの答えをありがとう。 –

0

あなたの応答の構造、簡単なものについてあなたは自分のポイントを印刷したい場合は、単に実際にループを使用することです:

for el in apiload: 
    print(el["championPoints"]) 

をあなたの問題が収集ポイントの合計を印刷する場合リスト内

from collections import Counter 

cnt = Counter() 
for col in apiload: 
    for k in col.keys(): 
     cnt[k] += c.get(k, 0) 

print(cnt['championPoints']) # should print the sum of championPoints 
+0

私はこのようなリストを使用することについて考えません。私に気づいてくれてありがとう。 –

関連する問題