[英]Combine two arrays into one JSON
I have array A["a","b","c"]
and B[1,2,3]
. 我有数组
A["a","b","c"]
和B[1,2,3]
。 I am trying to combine them into a single JSON file that has the following structure 我正在尝试将它们合并为具有以下结构的单个JSON文件
[{
"A": "a",
"B": 1
},
{
"A": "b",
"B": 2
},
{
"A": "c",
"B": 3
}
]
So far I have tried 到目前为止,我已经尝试过
data = {}
data['a'] = A
data['b'] = B
json_data = json.dumps(data)
print(json_data)
but that does not produce the result I want. 但这不会产生我想要的结果。
Any help or ideas are appreciated. 任何帮助或想法表示赞赏。
You can use zip
: 您可以使用
zip
:
a = ["a","b","c"]
b = [1,2,3]
result = [{'A':c, 'B':d} for c, d in zip(a, b)]
Output: 输出:
[{'A': 'a', 'B': 1}, {'A': 'b', 'B': 2}, {'A': 'c', 'B': 3}]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.