首页 > 解决方案 > python中列表和数组之间的转换

问题描述

嗨,我有一个接受以下一维向量结构的解码器,它从 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,但无法获得所需的结构。有出路吗 ?提示和建议将不胜感激。

标签: pythonarrayspython-3.xlistnumpy

解决方案


array = np.asarray(list)

list = np.ndarray.tolist(array)

推荐阅读