简体   繁体   English

如何访问始终在同一个 position 中但名称不同的字段?

[英]How to access a field that is always in the same position, but has a different name?

I am playing around with some soccer matchdata that is in JSON format, saved in the variable "matches" via我正在玩一些 JSON 格式的足球比赛数据,通过以下方式保存在变量“matches”中

with open('path') as json_data:
    matches = json.load(json_data)

The following is an excerpt of the data for match number 1 (gained via "matches[1]":以下是第 1 场比赛的数据摘录(通过“matches[1]”获得:

{
  "status": "Played",
  "roundId": 4405517,
  "gameweek": 34,
  "teamsData": {
    "2482": {
      "scoreET": 0,
      "coachId": 272299,
      "side": "home",
      "teamId": 2482,
      "score": 3,
      "scoreP": 0,
      "hasFormation": 1,
      "formation": {
        "bench": [

The name of the field "2482" is the ID of one of the two teams playing in the match (the other one follows later, but providing the whole match data for for even one match would make this post far too long).字段“2482”的名称是参加比赛的两支球队之一的 ID(另一支球队稍后会出现,但即使提供一场比赛的整个比赛数据也会使这篇文章太长)。 The data goes on like this (306 matches, 2 teams each), ie the field is always in the same position in the data.数据是这样进行的(306 场比赛,每场 2 支球队),即场总是在数据中的同一个 position 中。 Since the field's name is always different, however, I am struggling to access it for eg game number 20 without looking up the team's ID first since但是,由于该字段的名称总是不同的,因此我很难在没有首先查找团队 ID 的情况下访问它,例如第 20 场比赛

match[20]['teamsData'][1]

does not work ("KeyError: 1").不起作用(“KeyError:1”)。

Can anyone help me out?谁能帮我吗?

You can think of a dictionary as an index, and in your case you could just reindex the data with a more convenient value.您可以将字典视为索引,在您的情况下,您可以使用更方便的值重新索引数据。 If "side" is always either "home" or "away", you can use that known value instead of the variable team id value.如果“side”总是“home”或“away”,您可以使用该已知值而不是变量 team id 值。

In this example, "teamsData" is replaced with a reindexed dictionary.在此示例中,“teamsData”被重新索引的字典替换。

for match in matches:
    match["teamsData"] = {value["side"]:value for value in match["teamsData"].values()}

matches[20]["teamData"]["home"]

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在Django中声明一个表单字段,如果它与Python关键字同名? - How to declare a form field in Django, if it has the same name as a Python keyword? 如何访问硒中具有相同类名的第二个元素 - How to access the second element that has the same class name in selenium 如何访问与Python具有相同类名的第二个元素 - How to access the second element that has the same class name with Python 如何查询模型中与另一个模型的另一个字段同名的特定字段 - How to query a specific field in a model that has the same name as another field of another model 如果其他行在不同字段中具有相同值,则更改行中的字段 - Change field in row if other row has same value in different field 如何在Django中为具有相同访问权限但规则不同的组处理用户管理? - How to handle user mangement in Django for groups that has same access but different rules? 即使前缀字符串具有动态长度,也始终在相同位置打印字符串 - Print string in always the same position even if prefix-string has dynamic length 我们如何使用 inheritance 访问传递给具有相同名称但在两个不同类中的 function 的参数? - how we can use inheritance to access a parameter that is passed to a function with the same name but in two different classes? 如何以不同的名称序列化棉花糖字段 - How to serialize a Marshmallow field under a different name g“没有字段名称” - Wagtail “has no field name”
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM