从数组

时间:2016-03-26 19:49:18

标签: python json

我正在努力解决以下python问题。我想从现有列表创建一个json输出,但它是一个我无法实现的3级结构。

我知道如何创建一个2级结构:

data = json.dumps(
    [ {'book_id': i, 'exam': [{'count': k, 'wordids': w}]} \
        for k,i,w, in \
        zip(examcounts, bookid_list, wordid_list)],
    indent=4)

它产生一个有效的2级结构,但我需要以某种方式实现另一个for循环,我只是无法弄清楚如何。

所以希望的输出应该是这样的:

[
    {
        "book_id": "a",
        "exams": [
            {
                "exam": [
                    {
                        "result":"B"
                        "wordids": [
                            25201L,
                            41851L,
                            10134L
                        ]
                    }
                ],
                "exam": [
                    {
                        "result":"A"
                        "wordids": [
                            43,
                            41,
                            10
                        ]
                    }
                ]
             }
         ]
     },
     {
        "book_id": "b",
        "exams": [
            {
                "exam": [
                    {
                        "result":"C"
                        "wordids": [
                            25,
                            10
                        ]
                    }
                ],
                "exam": [
                    {
                        "result":"C"
                        "wordids": [
                            25,
                            10
                        ]
                    }
                ],
                "exam": [
                    {
                        "result":"F"
                        "wordids": [
                            43,
                            41,
                        ]
                    }
                ]
             }
         ]
     }
]

列出示例:

bookid_list: [u'a', u'b']
exmcounts: [22, 22, 20, 21, 21, 21, 21, 18]
results: ['B','A','C','C','F'...] with 8 length
wordid_list: [[25201L, 41851L, 10134L, 12174L, 6899L, 26078L, 12457L, 12176L, 26056L, 9451L, 25164L, 6618L, 43482L, 18681L, 12370L, 43272L, 15561L, 43483L, 30121L, 7715L, 21111L, 9461L], [13822L, 5172L, 7176L, 25754L, 12372L, 5509L, 16712L, 6187L, 8263L, 15408L, 20262L, 43487L, 5143L, 43488L, 6145L, 5697L, 7581L, 43489L, 22538L, 19417L, 43491L, 21498L], [19475L, 11631L, 12172L, 19495L, 30396L, 37274L, 43493L, 32393L, 5945L, 37521L, 6956L, 43494L, 36517L, 43495L, 13255L, 6558L, 12867L, 13382L, 6489L, 15447L]]

(wordid列表的长度为8,每个项目的长度为22,22,20,21 ..) 非常感谢提前!

2 个答案:

答案 0 :(得分:0)

尝试将您的数据打成3级词典。

dic = {'book_id': 'a', 'exams': {'exam1' : {'result': 'b', 'wordids': [25, 41, 10] } } }
data = json.dumps(dic)

只要所有类型都是JSON可序列化的,你就应该从3级字典中获得3级JSON。

答案 1 :(得分:0)

假设所有列表长度相同,您可以将每个列表添加到zip()并将每个列表嵌入到json字符串中。下面的示例包含三个元素的列表,可以根据需要扩展到8:

import json

bookid_list = [u'a', u'b', u'c']
examcounts = [22, 22, 20]
results = ['B','A','C']
wordid_list = [[25201, 41851, 10134, 12174, 6899, 26078, 12457, 12176, 26056,
               9451, 25164, 6618, 43482, 18681, 12370, 43272, 15561, 43483,
               30121, 7715, 21111, 9461],
              [13822, 5172, 7176, 25754, 12372, 5509, 16712, 6187, 8263,
               15408, 20262, 43487, 5143, 43488, 6145, 5697, 7581, 43489,
               22538, 19417, 43491, 21498],
              [19475, 11631, 12172, 19495, 30396, 37274, 43493, 32393, 5945,
               37521, 6956, 43494, 36517, 43495, 13255, 6558, 12867, 13382, 6489, 15447]]

data = json.dumps(
    [ {'book_id': b, 'exams': [{'exam': [{'results': r, 'wordids': w}]}]} \
        for b,e,r,w, in \
        zip(bookid_list, examcounts, results, wordid_list)],
    indent=4)

print(data)

<强>输出

[
    {
        "book_id": "a",
        "exams": [
            {
                "exam": [
                    {
                        "results": "B",
                        "wordids": [
                            25201,
                            41851,
                            10134,
                            12174,
                            6899,
                            26078,
                            12457,
                            12176,
                            26056,
                            9451,
                            25164,
                            6618,
                            43482,
                            18681,
                            12370,
                            43272,
                            15561,
                            43483,
                            30121,
                            7715,
                            21111,
                            9461
                        ]
                    }
                ]
            }
        ]
    },
    {
        "book_id": "b",
        "exams": [
            {
                "exam": [
                    {
                        "results": "A",
                        "wordids": [
                            13822,
                            5172,
                            7176,
                            25754,
                            12372,
                            5509,
                            16712,
                            6187,
                            8263,
                            15408,
                            20262,
                            43487,
                            5143,
                            43488,
                            6145,
                            5697,
                            7581,
                            43489,
                            22538,
                            19417,
                            43491,
                            21498
                        ]
                    }
                ]
            }
        ]
    },
    {
        "book_id": "c",
        "exams": [
            {
                "exam": [
                    {
                        "results": "C",
                        "wordids": [
                            19475,
                            11631,
                            12172,
                            19495,
                            30396,
                            37274,
                            43493,
                            32393,
                            5945,
                            37521,
                            6956,
                            43494,
                            36517,
                            43495,
                            13255,
                            6558,
                            12867,
                            13382,
                            6489,
                            15447
                        ]
                    }
                ]
            }
        ]
    }
]