Python 将两个不同的 Numpy 数组添加到嵌套字典
Python Add Two Different Numpy Arrays To Nested Dictionaries
好的,我正在研究对象检测模型。我可以成功检测到飞机。但我想将有关这些检测的信息发送给用户。
我有一个空字典:
planes= dict()
我有一个二维数组:
array1 = ([[ 20.2765, 20.2508, 545.9968, 51.9848],
[ 56.2883, 1240.3577, 385.5900, 1268.1694],
[ 502.5009, 1194.2896, 799.5756, 1221.9590],
[ 54.8439, 669.2018, 305.1923, 735.8934],
[ 502.3042, 424.8962, 735.9933, 712.6230]])
和另一个数组:
array2 = [1, 3, 3, 3, 3]
第一个数组是位面坐标,第二个数组是位面类型
我想形成一个JSON(或Python dict),其中这些坐标和类型对于每个检测到的平面相互连接。
planes= {
"plane1" : {
"plane_coordinates" : [20.2765, 20.2508, 545.9968, 51.9848],
"plane_type" : 1
},
"plane2" : {
"plane_coordinates" : [56.2883, 1240.3577, 385.5900, 1268.1694],
"plane_type" : 3
},
"plane3" : {
"plane_coordinates" : [502.5009, 1194.2896, 799.5756, 1221.9590],
"plane_type" : 3
},
"plane4" : {
"plane_coordinates" : [54.8439, 669.2018, 305.1923, 735.8934],
"plane_type" : 3
},
"plane" : {
"plane_coordinates" : [502.3042, 424.8962, 735.9933, 712.6230],
"plane_type" : 3
}
}
我已经为此苦苦挣扎了一个小时,但我的新手想不出可行的解决方案。
如果有人能帮助我,我将不胜感激。
鉴于上述问题:
import numpy as np
planes = dict()
names = ['Plane1','Plane2','Plane3','Plane4']
planes = dict()
names = ['Plane1','Plane2','Plane3','Plane4']
array1 = np.array([[ 20.2765, 20.2508, 545.9968, 51.9848],
[ 56.2883, 1240.3577, 385.5900, 1268.1694],
[ 502.5009, 1194.2896, 799.5756, 1221.9590],
[ 54.8439, 669.2018, 305.1923, 735.8934],
[ 502.3042, 424.8962, 735.9933, 712.6230]])
array2 = np.array([1, 3, 3, 3, 3])
for ns in range(len(names)):
planes[names[ns]] = {'Plane Coordinates': list(array1[ns]),
'Plane Type': array2[ns]}
这会生成所需的结果:
Planes = {'Plane1': {'Plane Coordinates': [20.2765, 20.2508, 545.9968, 51.9848],
'Plane Type': 1},
'Plane2': {'Plane Coordinates': [56.2883, 1240.3577, 385.59,
1268.1694],
'Plane Type': 3},
'Plane3': {'Plane Coordinates': [502.5009, 1194.2896,
799.5756, 1221.959],
'Plane Type': 3},
'Plane4': {'Plane Coordinates': [54.8439, 669.2018,
305.1923, 735.8934],
'Plane Type': 3}}
好的,我正在研究对象检测模型。我可以成功检测到飞机。但我想将有关这些检测的信息发送给用户。
我有一个空字典:
planes= dict()
我有一个二维数组:
array1 = ([[ 20.2765, 20.2508, 545.9968, 51.9848],
[ 56.2883, 1240.3577, 385.5900, 1268.1694],
[ 502.5009, 1194.2896, 799.5756, 1221.9590],
[ 54.8439, 669.2018, 305.1923, 735.8934],
[ 502.3042, 424.8962, 735.9933, 712.6230]])
和另一个数组:
array2 = [1, 3, 3, 3, 3]
第一个数组是位面坐标,第二个数组是位面类型
我想形成一个JSON(或Python dict),其中这些坐标和类型对于每个检测到的平面相互连接。
planes= {
"plane1" : {
"plane_coordinates" : [20.2765, 20.2508, 545.9968, 51.9848],
"plane_type" : 1
},
"plane2" : {
"plane_coordinates" : [56.2883, 1240.3577, 385.5900, 1268.1694],
"plane_type" : 3
},
"plane3" : {
"plane_coordinates" : [502.5009, 1194.2896, 799.5756, 1221.9590],
"plane_type" : 3
},
"plane4" : {
"plane_coordinates" : [54.8439, 669.2018, 305.1923, 735.8934],
"plane_type" : 3
},
"plane" : {
"plane_coordinates" : [502.3042, 424.8962, 735.9933, 712.6230],
"plane_type" : 3
}
}
我已经为此苦苦挣扎了一个小时,但我的新手想不出可行的解决方案。
如果有人能帮助我,我将不胜感激。
鉴于上述问题:
import numpy as np
planes = dict()
names = ['Plane1','Plane2','Plane3','Plane4']
planes = dict()
names = ['Plane1','Plane2','Plane3','Plane4']
array1 = np.array([[ 20.2765, 20.2508, 545.9968, 51.9848],
[ 56.2883, 1240.3577, 385.5900, 1268.1694],
[ 502.5009, 1194.2896, 799.5756, 1221.9590],
[ 54.8439, 669.2018, 305.1923, 735.8934],
[ 502.3042, 424.8962, 735.9933, 712.6230]])
array2 = np.array([1, 3, 3, 3, 3])
for ns in range(len(names)):
planes[names[ns]] = {'Plane Coordinates': list(array1[ns]),
'Plane Type': array2[ns]}
这会生成所需的结果:
Planes = {'Plane1': {'Plane Coordinates': [20.2765, 20.2508, 545.9968, 51.9848],
'Plane Type': 1},
'Plane2': {'Plane Coordinates': [56.2883, 1240.3577, 385.59,
1268.1694],
'Plane Type': 3},
'Plane3': {'Plane Coordinates': [502.5009, 1194.2896,
799.5756, 1221.959],
'Plane Type': 3},
'Plane4': {'Plane Coordinates': [54.8439, 669.2018,
305.1923, 735.8934],
'Plane Type': 3}}