【问题标题】:conversion between lists and arrays in pythonpython中列表和数组之间的转换
【发布时间】:2019-04-19 23:32:24
【问题描述】:

您好,我有一个解码器,它接受以下一维向量结构,它从 json 文件中读取

[423.99115959756097, 113.56845980228232, 429.4810943320526, 105.55707869260885, 411.09109879247507, 105.04387943256503, 444.51020250865184, 111.46554256405251, 395.02871954830175, 110.45595140768442, 455.7746649948708, 153.06139303597925, 392.6237135301358, 151.58618644012543, 496.1948133052633, 198.7240759060008, 388.61044648280836, 201.91381256375186, 476.5792881572282, 189.77835703320605, 414.4238280839665, 217.0464906777402, 456.24481798279527, 248.5199288727622, 420.09541327592524, 246.70985202053535, 465.8580365449809, 317.1224158312518, 406.8982403412771, 309.4914702916358, 458.6168283920967, 351.68948527259715, 402.61231695721335, 348.2695940057319, 0.0, 0.0]

在发送端进行以下实现

padding_matrix = np.zeros((18, 2))
padding_matrix[:instance_keypoint_coords.shape[0],:instance_keypoint_coords.shape[1]] = instance_keypoint_coords
reshape_matrix = np.reshape(padding_matrix,(1,36))

#create the dictionary structure 
my_dict = {}
new_dict = {}
my_dict["pose_keypoints_2d"] = reshape_matrix
new_dict['people'] = my_dict

# json_data = json.dumps(new_dict['people']["pose_keypoints_2d"].tolist())
#print(json_data)
#Dump the json file to file object 
with open('2D_detections.json', 'w') as fp:
    json.dump((new_dict['people']["pose_keypoints_2d"].tolist()),fp)`

json文件的示例输出如下

[[102.14354944229126, 278.29406929016113, 93.47736525535583, 286.3470059633255, 92.25288438796997, 271.6073254644871, 94.68143308162689, 296.2039098739624, 94.90646886825562, 269.15104579925537, 122.17410278320312, 316.0685751438141, 124.30206298828125, 269.9444923400879, 96.03413963317871, 374.60106086730957, 131.26836967468262, 269.6517162322998, 58.05227613449097, 350.7924575805664, 94.16577589511871, 279.83956146240234, 232.34951972961426, 319.1662890315056, 223.5771770477295, 281.9341802597046, 291.25416827201843, 330.24444103240967, 301.2301368713379, 274.06850385665894, 331.83830547332764, 334.17417335510254, 337.0342251062393, 275.64743542671204, 0.0, 0.0]]

因此,我必须创建一个类似于解码器的一维输出。我尝试使用函数toflat,但无法获得所需的结构。有出路吗 ?提示和建议将不胜感激。

【问题讨论】:

    标签: python arrays python-3.x list numpy


    【解决方案1】:
    array = np.asarray(list)
    
    list = np.ndarray.tolist(array)
    

    【讨论】:

    • 不鼓励仅使用代码的答案。请添加一些解释,说明这是如何解决问题的,或者这与现有答案有何不同。 From Review
    【解决方案2】:

    如果你只是想要一个 Python 列表,那么:

    list_of_list = [[102.14354944229126, 278.29406929016113, 93.47736525535583, 286.3470059633255, 92.25288438796997, 271.6073254644871, 94.68143308162689, 296.2039098739624, 94.90646886825562, 269.15104579925537, 122.17410278320312, 316.0685751438141, 124.30206298828125, 269.9444923400879, 96.03413963317871, 374.60106086730957, 131.26836967468262, 269.6517162322998, 58.05227613449097, 350.7924575805664, 94.16577589511871, 279.83956146240234, 232.34951972961426, 319.1662890315056, 223.5771770477295, 281.9341802597046, 291.25416827201843, 330.24444103240967, 301.2301368713379, 274.06850385665894, 331.83830547332764, 334.17417335510254, 337.0342251062393, 275.64743542671204, 0.0, 0.0]]
    
    list_1D = list_of_list[0]
    

    但是,如果你想要一个 1D numpy 数组,那么:

    arr = np.array(list_of_list).squeeze()
    

    另外,当您从序列化的 json 中读取时,您可以简单地使用 numpy 数组而不是转换为 Python 列表:

    json_data_1D = json.dumps(new_dict['people']["pose_keypoints_2d"]).squeeze()
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-11-04
      • 1970-01-01
      • 1970-01-01
      • 2019-01-23
      • 2020-03-23
      相关资源
      最近更新 更多