【发布时间】:2022-12-09 07:25:15
【问题描述】:
我有一些数据
types = ['type1', 'type2', 'type3']
names = ['name1', 'name2', 'name3']
我想将它作为嵌套字符串集加载到 JSON 中,看起来像这样
{
labels{
type1:name1
type2:name2
}
}
我尝试了一些事情,包括
labels={types:names} (didnt work)
labels=json.loads{types:names} (various versions of this)
labels=json.dumps{types:names} (various versions of this)
labels=json.loads(json.dumps(types:names)) (I was getting desperate here)
问了一个朋友,他说在python的jsonlib中只能对键值使用变量,不能对键名使用变量。有没有比遍历所有类型和名称更好的方法呢?
【问题讨论】: