python - 在列表中,从 "team"中没有 "all_players"的玩家处获取 "name"

标签 python

我的问题是关于我拥有的这个列表。列表中有两件事:“元数据”和“玩家”,“玩家”中的“玩家”是“all_players”。现在我想获取玩家姓名和玩家团队,但我不知道如何做到这一点。我可以做 data["data"][i]['players']['all_players'][{name}] 但我只有名字,当我不知道我应该选择哪支球队的球员。

这是我的列表:

{
    "data": [
        {
            "metadata": {
                "map": "Breeze",
                "game_length": 1702289,
                "game_start": 1657464507,
                "game_start_patched": "Sunday, July 10, 2022 4:48 PM",
                "rounds_played": 20,
                "mode": "Competitive",
                "queue": "Standard",
                "platform": "PC",
                "region": "eu",
                "cluster": "London"
            },
            "players": {
                "all_players": [
                    {
                        "puuid": "70f7373a-ba78-5c9a-a963-767fbf27d9ad",
                        "name": "Player 1",
                        "team": "Blue"
                    },
                    {
                        "puuid": "70f7373a-ba78-5c9a-a963-767fbf27d9ad",
                        "name": "Player 2",
                        "team": "Blue"
                    },
                    {
                        "puuid": "70f7373a-ba78-5c9a-a963-767fbf27d9ad",
                        "name": "Player 3",
                        "team": "Red"
                    },
                ]}
        }]
}

这只是一小部分,里面的东西都是我想要的。列表更长,但对于这部分来说没有必要。此列表或数据来自 JSON。

最佳答案

这里有一种方法可以满足您的问题:

all_players = data["data"][0]["players"]["all_players"]
playerNames = [player["name"] for player in all_players]
teamNames = [player["team"] for player in all_players]
teamByPlayer = {player["name"]:player["team"] for player in all_players}

输出

playerNames
['Player 1', 'Player 2', 'Player 3']

teamNames
['Blue', 'Blue', 'Red']

teamByPlayer
{'Player 1': 'Blue', 'Player 2': 'Blue', 'Player 3': 'Red'}

说明:

  • playerNames 的作业和teamNames每个使用 list comprehension 根据 for 中的项目创建列表循环。
  • teamNames 的分配使用 dict comprehension 使用 key player["name"] 添加键:值对和值player["team"]对于每个 playerfor循环。

要查找给定球员的球队,您可以使用 accumulate() 来执行此操作:

from itertools import accumulate
def findTeamOfPlayer(player):
    all_players = data["data"][0]["players"]["all_players"]
    team = list(accumulate(p["team"] for p in all_players if p["name"] == player))
    return team[0] if team else None

target = "Player 2"
team = findTeamOfPlayer("Player 2")
print(team)
team = findTeamOfPlayer("Player 99")
print(team)

输出:

Blue
None

关于python - 在列表中,从 "team"中没有 "all_players"的玩家处获取 "name",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/72931469/

相关文章:

python - urllib.urlopen 返回一个旧页面?

python - PyQt 中的线程

python - 只读字段没有保存在openerp的数据库中?

python - 如何在python中下载zip文件并从中解析csv文件

Python flask : search MySQL start with an alphabet

python - 使用类实例作为类属性、描述符和特性

javascript - 如何通过匹配 BeautifulSoup 中 href 属性中的文本来获取元素

python - 弹出列表 - 典型的 socks 对问题 (Python)

python - Holoviews:如何用时间索引绘制 DataFrame

python - django 在 Raspberry PI 中使用热敏打印机