在词典中导航



我无法导航从这里加载 json 生成的字典。有人可以帮我吗?谢谢。我试过tier_d[test - 1] = data[summoners[test - 1]['entries']['leaguePoints']]但它告诉我expected integers.我想从中获取"联赛积分"。

Summoners_id是从另一个请求提供的 ID,它们看起来像"24657246",例如

url = 'https://{0}.api.pvp.net/api/lol/{0}/v2.5/league/by-summoner/{1},{2},{3},{4},{5},{6},{7},{8},{9},{10}/entry?api_key={11}'.format(region, summoner_id_1, summoner_id_2, summoner_id_3, summoner_id_4, summoner_id_5, summoner_id_6, summoner_id_7, summoner_id_8, summoner_id_9, summoner_id_10, key)
print(url)
response = requests.get(url)
data = json.loads(response.text)
tier = ['Unranked', 'Unranked', 'Unranked', 'Unranked', 'Unranked', 'Unranked', 'Unranked', 'Unranked', 'Unranked', 'Unranked']
tier_d = ['', '', '', '', '', '', '', '', '', '']
done = False
test = 0
rankeds = []
for ids in data:
    rankeds.append(ids)
    print(rankeds)
print(summoners)
while test <= 9:
    test += 1
    if summoners[test - 1] in rankeds:
        tier_d[test - 1] = data[summoners[test - 1]['entries']['leaguePoints']]
        tier[test - 1] = data[summoners[test - 1]][0]['tier']
        print(data[summoners[test - 1]][0]['tier'])
    print(tier)

你有一些listsdictionaries嵌套在一起。你必须仔细检查它们。

data[summoners[test - 1]]的输出为:

[{'entries': [{'division': 'I',
    'isFreshBlood': False,
    'isHotStreak': False,
    'isInactive': False,
    'isVeteran': False,
    'leaguePoints': 0,
    'losses': 14,
    'playerOrTeamId': 'xxyyzz',
    'playerOrTeamName': 'xxyyzz',
    'wins': 8}],
  'name': "Talon's Army",
  'queue': 'xxyyzz',
  'tier': 'BRONZE'},
 {'entries': [{'division': 'V',
    'isFreshBlood': False,
    'isHotStreak': False,
    'isInactive': False,
    'isVeteran': False,
    'leaguePoints': 18,
    'losses': 2,
    'playerOrTeamId': 'xxyyzz',
    'playerOrTeamName': 'xxyyzz',
    'wins': 5}],
  'name': "xxyyzz",
  'queue': 'xxyyzz',
  'tier': 'SILVER'}]

因此,这应该为您提供leaguePoints值:

tier_d[test - 1] = data[summoners[test - 1]][0]['entries'][0]['leaguePoints']
tier_d[test - 1] = data[summoners[test - 1]][1]['entries'][0]['leaguePoints']

取决于列表中的第一个或第二个条目。

相关内容

  • 没有找到相关文章

最新更新