将列表数组转换为字典数组

时间:2019-05-30 11:44:16

标签: python json

我有以下数据

{"array": [[1288.1639933233453], [1255.1777295971826], 
[1250.1075681742996], [1283.9962801780075], [1297.4411412000093]]}

我想按如下方式转换上述数据

[{"value": 1288.1639933233453},
{"value": 1255.1777295971826},
{"value": 1250.1075681742996},
{"value": 1283.9962801780075},
{"value": 1297.4411412000093}]

7 个答案:

答案 0 :(得分:1)

d = {"array": [[1288.1639933233453], [1255.1777295971826],
               [1250.1075681742996], [1283.9962801780075], [1297.4411412000093]]}
d = [{"value": i[0]} for i in d["array"]]
print(d)

输出:

[{'value': 1288.1639933233453}, {'value': 1255.1777295971826}, {'value': 1250.1075681742996}, {'value': 1283.9962801780075}, {'value': 1297.4411412000093}]

答案 1 :(得分:1)

x = {"array": [[1288.1639933233453], [1255.1777295971826], 
[1250.1075681742996], [1283.9962801780075], [1297.4411412000093]]}
y = [{'value' : i[0]} for i in x['array']]
print(y)

答案 2 :(得分:0)

d1 = {"array": [[1288.1639933233453], [1255.1777295971826], [1250.1075681742996], [1283.9962801780075], [1297.4411412000093]]}
vals = d1["array"]
l = []
for val in vals:
    l.append({"value": val[0]})
print(l)

[{'value': 1288.1639933233453},
 {'value': 1255.1777295971826},
 {'value': 1250.1075681742996},
 {'value': 1283.9962801780075},
 {'value': 1297.4411412000093}]

答案 3 :(得分:0)

使用lambda

a={"array": [[1288.1639933233453], [1255.1777295971826], 
[1250.1075681742996], [1283.9962801780075], [1297.4411412000093]]}

res = list(map(lambda x:{'value':x[0]},a['array']))

答案 4 :(得分:0)

您没有提到列表包含列表将始终包含1个元素,因此,我认为最好遍历所有列表包含列表理解而不遗漏值:

d = {"array": [[1288.1639933233453], [1255.1777295971826], [1250.1075681742996], [1283.9962801780075], [1297.4411412000093]]}

r = [{"value": v} for e in d["array"] for v in e]

输出:

[{'value': 1288.1639933233453}, {'value': 1255.1777295971826}, {'value': 1250.1075681742996}, {'value': 1283.9962801780075}, {'value': 1297.4411412000093}]

答案 5 :(得分:0)

您可以使用以下代码:

myDic = {"array": [[1288.1639933233453], [1255.1777295971826], 
[1250.1075681742996], [1283.9962801780075], [1297.4411412000093]]}

myList = []

for i in myDic['array']:
    value = {'value':i[0]}
    myList.append(value)

print(myList)

OR

使用pythonic方法:)

myDic = {"array": [[1288.1639933233453], [1255.1777295971826], 
[1250.1075681742996], [1283.9962801780075], [1297.4411412000093]]}

myList = [{'value':i[0]} for i in myDic['array']]

print(myList)

答案 6 :(得分:0)

您可以这样做:

values = {"array": [[1288.1639933233453], [1255.1777295971826], 
[1250.1075681742996], [1283.9962801780075], [1297.4411412000093]]}

values = [{"value: i[0]} for i in values["array"]]