Python - 如何从一个dicts列表中获取一个项目?

时间:2016-11-22 06:35:01

标签: python json

现在(如果我没错的话)我有一份含有一些信息的词典清单;我想从列表中获取一个特定的项目,但是我遇到问题需要达到我想要的项目;这就是我正在尝试的:

stats = requests.get("https://lan.api.pvp.net/api/lol/lan/v1.3/stats/by-summoner/24244/"
                     "summary?season=SEASON2016&api_key=").json()

pprint([d['playerStatSummaries'] for d in stats if 'playerStatSummaryType' in d])

我通过观察一些例子来尝试;它可能会工作,但也许我正在以错误的方式做,输出只是一个“[]”请求有这个信息:

"summonerId": 24244,
"playerStatSummaries": [

    {
        "playerStatSummaryType": "CAP5x5",
        "wins": 20,
        "modifyDate": 1453305771000,
        "aggregatedStats": {
            "totalChampionKills": 308,
            "totalMinionKills": 4985,
            "totalTurretsKilled": 36,
            "totalNeutralMinionsKilled": 466,
            "totalAssists": 255
        }
    },
    {
        "playerStatSummaryType": "CoopVsAI",
        "wins": 34,
        "modifyDate": 1453305771000,
        "aggregatedStats": {
            "totalChampionKills": 394,
            "totalMinionKills": 2741,
            "totalTurretsKilled": 57,
            "totalNeutralMinionsKilled": 146,
            "totalAssists": 336
        }
    },
    {
        "playerStatSummaryType": "CoopVsAI3x3",
        "wins": 1,
        "modifyDate": 1453305771000,
        "aggregatedStats": {
            "totalChampionKills": 6,
            "totalMinionKills": 69,
            "totalTurretsKilled": 2,
            "totalNeutralMinionsKilled": 0,
            "totalAssists": 3
        }
    },
    {
        "playerStatSummaryType": "RankedTeam3x3",
        "wins": 0,
        "losses": 0,
        "modifyDate": 1353893998000,
        "aggregatedStats": { }
    },
    {
        "playerStatSummaryType": "RankedTeam5x5",
        "wins": 0,
        "losses": 0,
        "modifyDate": 1354061714000,
        "aggregatedStats": { }
    }

假设我要访问第二个“CoopVsAI”的信息;从那我想要访问“totalChampionKills”;我怎样才能做到这一点?因为它们是嵌套的,所以我不确定如何做到这一点。我试着做了

for key, value in stats['playerStatSummaries'].items():
    if key['playerStatSummaryType'] == "Unranked":
        # do something

但那不起作用,所以;就是这样,感谢您提供的任何帮助:)

3 个答案:

答案 0 :(得分:1)

"playerStatSummaries"保留list值,而不是dict。因此,您的for循环应该是:

#         v  This will be `dict` object
for player_stat_summary in stats['playerStatSummaries']:
    if player_stat_summary['playerStatSummaryType'] == "Unranked":
        # Do Something

        # player_stat_summary['aggregatedStats']['totalChampionKills']
        # For accessing the value you require

答案 1 :(得分:1)

假设您的stats

stats ={"summonerId": 24244,
"playerStatSummaries": [

    {
        "playerStatSummaryType": "CAP5x5",
        "wins": 20,
        "modifyDate": 1453305771000,
        "aggregatedStats": {
            "totalChampionKills": 308,
            "totalMinionKills": 4985,
            "totalTurretsKilled": 36,
            "totalNeutralMinionsKilled": 466,
            "totalAssists": 255
        }
    },
    {
        "playerStatSummaryType": "CoopVsAI",
        "wins": 34,
        "modifyDate": 1453305771000,
        "aggregatedStats": {
            "totalChampionKills": 394,
            "totalMinionKills": 2741,
            "totalTurretsKilled": 57,
            "totalNeutralMinionsKilled": 146,
            "totalAssists": 336
        }
    },
    {
        "playerStatSummaryType": "CoopVsAI3x3",
        "wins": 1,
        "modifyDate": 1453305771000,
        "aggregatedStats": {
            "totalChampionKills": 6,
            "totalMinionKills": 69,
            "totalTurretsKilled": 2,
            "totalNeutralMinionsKilled": 0,
            "totalAssists": 3
        }
    },
    {
        "playerStatSummaryType": "RankedTeam3x3",
        "wins": 0,
        "losses": 0,
        "modifyDate": 1353893998000,
        "aggregatedStats": { }
    },
    {
        "playerStatSummaryType": "RankedTeam5x5",
        "wins": 0,
        "losses": 0,
        "modifyDate": 1354061714000,
        "aggregatedStats": { }
    }
    ]
}

以下内容可行

for stat in stats['playerStatSummaries']:
    if(stat["playerStatSummaryType"] == 'CoopVsAI'):
        print stat["aggregatedStats"]['totalChampionKills']

答案 2 :(得分:0)

" playerStatSummaries"这是一个内部对象列表,为什么不直接读作元素列表?

In [9]: a[1]
Out[9]: 
{'aggregatedStats': {'totalAssists': 336,
  'totalChampionKills': 394,
  'totalMinionKills': 2741,
  'totalNeutralMinionsKilled': 146,
  'totalTurretsKilled': 57},
 'modifyDate': 1453305771000,
 'playerStatSummaryType': 'CoopVsAI',
 'wins': 34}

如果你需要更深层次的是同样的操作

In [12]: a[1]['aggregatedStats']['totalChampionKills']
Out[12]: 394