Python 列表中的列表-如何访问元素

Python 列表中的列表-如何访问元素,python,list,dictionary,Python,List,Dictionary,这是我的代码: 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) 这就是我从我的请求中得到的信息。获取: { '

这是我的代码:

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)
这就是我从我的请求中得到的信息。获取:

{
    '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'])

从dictionary对象获取值很简单

打印项目['participants'][0]。获取('teamId')


打印项目['participants'][0]。获取('spell1Id')

字典中有一个列表,而不是列表中的列表。一旦你访问了完整的列表,你就可以像其他任何列表一样索引到其中。而且,你的例子中的
参与者
列表中只有一个元素。我们是否应该假设可能存在多个元素?如果是,您希望访问哪些元素?第一个?所有这些,一个接一个?或者什么?可能是@jornsharpe的副本也许我的字典里有一本字典没有?但是我怎样才能访问这个?我试着这样做:打印(请求参数['participants']['gameId']),但不起作用。你有一个字典在一个字典的列表中,没有可能。你所犯的错误告诉了你为什么这不起作用。非常感谢你,先生!第二个例子正是我要找的!干杯