Python:将多个列表合并到一个JSON数组中

Python:将多个列表合并到一个JSON数组中,json,python-3.x,list,dictionary,merge,Json,Python 3.x,List,Dictionary,Merge,我想将多个列表合并到一个JSON数组中。 以下是我的两份清单: address = ['address1','address2'] temp = ['temp1','temp2'] 我通过下面的调用合并了这两个列表,并创建了一个JSON new_list = list(map(list, zip(address, temp))) jsonify({ 'data': new_list }) 这是我的通话结果: { "data": [ [

我想将多个列表合并到一个JSON数组中。 以下是我的两份清单:

address =  ['address1','address2']
temp = ['temp1','temp2']
我通过下面的调用合并了这两个列表,并创建了一个JSON

new_list = list(map(list, zip(address, temp)))
jsonify({
    'data': new_list
})
这是我的通话结果:

{
    "data": [
        [
            "address1",
            "temp1"
        ],
        [
            "address2",
            "temp2"
        ]
    ]
}
然而,我想收到以下问题。如何操作以及如何插入标识符
地址
你好

{
    "data": [
        {
            "address": "address1",
            "temp": "temp1"
        },
        {
            "address": "address2",
            "temp": "temp2"
        }
    ]
}

您可以使用列表:

import json

address =  ['address1','address2']
temp = ['temp1','temp2']

d = {'data': [{'address': a, 'temp': t} for a, t in zip(address, temp)]}

print( json.dumps(d, indent=4) )
印刷品:

{
    "data": [
        {
            "address": "address1",
            "temp": "temp1"
        },
        {
            "address": "address2",
            "temp": "temp2"
        }
    ]
}

您可以使用列表:

import json

address =  ['address1','address2']
temp = ['temp1','temp2']

d = {'data': [{'address': a, 'temp': t} for a, t in zip(address, temp)]}

print( json.dumps(d, indent=4) )
印刷品:

{
    "data": [
        {
            "address": "address1",
            "temp": "temp1"
        },
        {
            "address": "address2",
            "temp": "temp2"
        }
    ]
}

您可以像这样更改现有代码。该
lambda
函数将实现将其转换为dict的技巧

address =  ['address1','address2']
temp = ['temp1','temp2']

new_list = list(map(lambda x : {'address': x[0], 'temp': x[1]}, zip(address, temp)))

jsonify({
    'data': new_list
})


您可以像这样更改现有代码。该
lambda
函数将实现将其转换为dict的技巧

address =  ['address1','address2']
temp = ['temp1','temp2']

new_list = list(map(lambda x : {'address': x[0], 'temp': x[1]}, zip(address, temp)))

jsonify({
    'data': new_list
})


很酷,很简单。我本来可以自己想出来的。非常感谢你的帮助。真的很棒,很酷很简单。我本来可以自己想出来的。非常感谢你的帮助。真的很好。