【问题标题】:Python Add Two Different Numpy Arrays To Nested DictionariesPython 向嵌套字典中添加两个不同的 Numpy 数组
【发布时间】:2020-09-18 16:59:27
【问题描述】:

好的,我正在研究对象检测模型。我可以成功探测到飞机。但我想将有关这些检测的信息发送给用户。

我有一本空字典:

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 字典),在该 JSON(或 Python 字典)上,每个检测到的平面的这些坐标和类型都相互连接。

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
  }
}

我已经为此苦苦挣扎了一个小时,但我的新手头脑无法想出一个可行的解决方案。

如果有人可以帮助我,我将不胜感激。

【问题讨论】:

    标签: arrays json python-3.x numpy object-detection


    【解决方案1】:

    鉴于上述问题:

     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}}
    

    【讨论】:

    • 如果上述解决方案回答了您的问题,您能否将查询标记为已关闭?
    • 在 Stack Overflow 上,“关闭问题”意味着问题被搁置并且无法回答,直到它被编辑到它确认 SO 规则并提供足够的信息来回答的状态。也许您想要求接受答案,但这也不意味着它已关闭(应该避免,因为接受答案不是强制性的,完全取决于用户)。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-11-07
    • 2015-12-31
    • 2023-02-05
    • 2020-03-13
    • 2018-04-25
    • 1970-01-01
    相关资源
    最近更新 更多