需要从2个数组构造一个JSON



我需要用双键键=>VALUE填充JSON,其中键是从0到9的数字和值是"(#)"之间的相同数字,我如何构建JSON??

import json
import sys
keys = []
values = []
for i in range(10):
number = i+1
letter= str(numerito)
keys.insert(i,number)
values.insert(i,"("+letter+")")
print(keys)
print(values)
#Here I need to construct the JSON
json = {}
json = ('key{}'.format(index), val) for keys, val in enumerate(value{}) #I can't understand how to make the association here
#...And y want to obtain some like that:
"""
json = {0:"(0)",1:"(1)",2:"(2)",3:"(3)",4:"(4)",5:"(5)",6:"(6)",7:"(7)",8:"(8)",9:"(9)",}
"""

直接将键值对添加到循环中的字典中。

import json
d = {}
for i in range(10):
d[i] = "(" + str(i) + ")"
print(d)
print(json.dumps(d)) # convert dictionary to JSON string

可以使用字典推导式缩短代码。

d = {i : f'({i})' for i in range(10)}

相关内容

  • 没有找到相关文章

最新更新