【问题标题】:How to create structured JSON object from lists?如何从列表创建结构化 JSON 对象?
【发布时间】:2019-03-23 15:31:56
【问题描述】:

我正在尝试从我正在抓取的数据中创建一个结构化的 JSON 对象。

我抓取的数据存储在几个列表中。 list1 包含事件名称,list2 包含事件类别,list3 包含原始事件数据(与事件类别存储顺序相同)

list1 = [['event1'], ['event2'], ['event3']]
list2 = [['team' , 'score', 'date'], ['team', 'location', 'date'], ['team', ' record']]
list3 = [['team1' , 'score1', 'date1', 'team2' , 'score2', 'date2'],
         ['team1' , 'location1', 'date1', 'team2' , 'location2', 'date2'],
         ['team1', 'record 1', 'team2', 'record2', 'team3', 'record3']]

我正在尝试创建格式为的 json 文件:

{
  "event1": {
          "1": {
                  "team": team1,
                  "score": score1,
                  "date": date1,
               },
          "2": {
                  "team": team2,
                  "score": score2,
                  "date": date2,
               },

            },
  "event2": {
          "1": {
                  "team": team1,
                  "location": location1,
                  "date": date1,
               },
          "2": {
                  "team": team2,
                  "location": location2,
                  "date": date2,
               },
  "event3": {
          "1": {
                  "team": team1,
                  "record": record1,
               },
          "2": {
                  "team": team2,
                  "record": record2,
               },
          "3": {
                  "team": team3,
                  "record": record3,
               },
}

在 python 上甚至可以以这种方式进行结构化吗?我可以使用其他语言的 for 循环来做到这一点,但我对如何使用 python 附加数据感到头疼。

【问题讨论】:

  • 你怎么知道 team3 属于 event3 而不是 event1 或 2?
  • @LucasWieloch 因为list1list2list3对齐的,所以list3中的每个列表对应list中的每个列表(长度一个列表)

标签: python arrays json object append


【解决方案1】:

希望你能理解我的解决方案,我评论了它。

list1 = [['event1'], ['event2'], ['event3']]
list2 = [['team' , 'score', 'date'], ['team', 'location', 'date'], ['team', ' record']]
list3 = [['team1' , 'score1', 'date1', 'team2' , 'score2', 'date2'], ['team1' , 'location1', 'date1', 'team2' , 'location2', 'date2'], ['team1', 'record 1', 'team2', 'record2', 'team3', 'record3']]
out = {}
#iterate over each of the lists together
for (a,), b, c in zip(list1, list2, list3):
    #initialise the "event." attribute in the `out` dict
    out[a] = {}
    #chunk the lists in `list3` to the size of the lists in `list2` and iterate
    for i, l in enumerate([c[i:i+len(b)] for i in range(0, len(c), len(b))]):
        #for this chunk, initiate a sub dictionary under the "event." attr in `out`
        out[a][str(i+1)] = {}
        #iterate over the elements in the list from `list2`
        for j, e in enumerate(b):
            #assign the corresponding chunk element to the appropriate location.
            out[a][str(i+1)][e] = l[j]

给出:

{
    "event1": {
        "1": {
            "team": "team1",
            "score": "score1",
            "date": "date1"
        },
        "2": {
            "team": "team2",
            "score": "score2",
            "date": "date2"
        }
    },
    "event2": {
        "1": {
            "team": "team1",
            "location": "location1",
            "date": "date1"
        },
        "2": {
            "team": "team2",
            "location": "location2",
            "date": "date2"
        }
    },
    "event3": {
        "1": {
            "team": "team1",
            " record": "record 1"
        },
        "2": {
            "team": "team2",
            " record": "record2"
        },
        "3": {
            "team": "team3",
            " record": "record3"
        }
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-03-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-11-24
    • 2017-05-06
    • 2021-12-05
    相关资源
    最近更新 更多