我在python脚本中有这样的字典
{key:value}
{key:value}
{key:value}
我想把它做成一个像
这样的单键字典。[{'key':value} , {'key:value} , {'key':value}......]
我的代码如下:
for (recActorIMDBid, recActorName, recActorTopTen) in cursor:
recActorTopTen = str(recActorTopTen).replace(",", "_ ")
contentIDArray =[]
contentIDArray = recActorTopTen.split("_ ")
if len(contentIDArray) > 9:
stringOut = "ID," + str(recID) + ",IMDB ID," + str(recActorIMDBid) + ",NAME," + str(recActorName) + ",TOP TEN," \ + str(recActorTopTen)
print(stringOut)
我需要将结果更改为json,我该怎么办?
答案 0 :(得分:0)
这将起作用:
dict1 = {0: 5, 1: 8, 2: 3}
dict2 = {0: 6, 1: 2, 2: 4}
dict3 = {0: 7, 1: 9, 2: 2}
dicts = []
dicts.append(dict1)
dicts.append(dict2)
dicts.append(dict3)
答案 1 :(得分:0)
只需遍历items()
(键值对)并使用列表理解
[{k: v} for k, v in my_dict.items()]
注意:我假设您的问题有一些错别字,因为您描述的内容并不完全正确。