如何访问另一个列表中的字典列表

问题描述 投票:0回答:1

我是 Python 新手,需要一些帮助。

我正在尝试访问列表“季节”的值,然后创建一个数据框来解析数据。实际上,我想做的是检索联赛、国家/地区和赛季的键和值,创建一个数据框,其中列名称是键,并用值填充所有行。我有一个包含联赛和国家/地区值的数据框,但我正在努力获取本赛季的值。有人可以帮我吗?

这是json文件:

{
  "get": "leagues",
  "parameters": [],
  "errors": [],
  "results": 1072,
  "paging": {
    "current": 1,
    "total": 1
  },
  "response": [
    {
      "league": {
        "id": 4,
        "name": "Euro Championship",
        "type": "Cup",
        "logo": "https://media.api-sports.io/football/leagues/4.png"
      },
      "country": {
        "name": "World",
        "code": null,
        "flag": null
      },
      "seasons": [
        {
          "year": 2008,
          "start": "2008-06-07",
          "end": "2008-06-29",
          "current": false,
          "coverage": {
            "fixtures": {
              "events": true,
              "lineups": true,
              "statistics_fixtures": false,
              "statistics_players": false
            },
            "standings": false,
            "players": true,
            "top_scorers": true,
            "top_assists": true,
            "top_cards": true,
            "injuries": false,
            "predictions": true,
            "odds": false
          }
        },
        {
          "year": 2012,
          "start": "2012-06-08",
          "end": "2012-07-01",
          "current": false,
          "coverage": {
            "fixtures": {
              "events": true,
              "lineups": true,
              "statistics_fixtures": false,
              "statistics_players": false
            },
            "standings": false,
            "players": true,
            "top_scorers": true,
            "top_assists": true,
            "top_cards": true,
            "injuries": false,
            "predictions": true,
            "odds": false
          }
python pandas list dictionary nested
1个回答
-1
投票
jsonFile = *yourJson*
response = jsonFile.get('response') #Get the value inside the 'response' key, which is a list
seasons = response[0].get('seasons') #In the response your data "seasons" is inside the first dict element (Index zero), then you get the value the same way we did above.
© www.soinside.com 2019 - 2024. All rights reserved.