这是我的代码:
def liveGame(summonerName):
req = requests.get('https://br1.api.riotgames.com/lol/spectator/v3/active-games/by-summoner/' + str(summonerName) + '?api_key=' + apikey)
req_args = json.loads(req.text)
print(req_args)
这就是我从 request.get 收到的:
{
'gameId': 1149933395,
'mapId': 11,
'participants': [
{
'teamId': 100,
'spell1Id': 11,
'spell2Id': 4,
'championId': 141,
'profileIconId': 7,
'summonerName': 'Disneyland Party',
...
}
]
}
我已经简化了请求的返回,但是正如您所看到的,“参与者”索引是另一个列表。那么,如何访问此列表的内容(teamId、Spell1Id 等)?
我只能通过这种方式访问完整列表:
print(req_args['participants'])
但是,我想要做的是仅访问“参与者”列表中的一个元素。
我正在使用 Python 3.6。
您可以像访问普通列表一样使用索引访问此列表项如果您想访问 req_args['participants'] 的第一个元素,您可以使用
req_args['participants'][i]
其中 i 只是您要从列表中访问的项目的索引。
由于链表中的项目是字典,用于访问只有一个项目(在本例中为第一项目)的 teamId 和 spellId,您可以执行以下操作
req_args['participants'][0]['teamId']
req_args['participants'][0]['spell1Id']
您还可以遍历列表以访问每个字典和 teamId、spell1Id 或字典中存在的其他键的值,如下所示
for participant in req_args['participants']:
print(participant['teamId'])
print(participant['spell1Id'])
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句