| | |
| | | # 将 dynamicMatrix 转换为 numpy 结构化数组 |
| | | def convert_to_structured_array(dynamicMatrix): |
| | | # 定义结构化数组的 dtype |
| | | dtype = [('serial', int), ('vehicle', 'U2')] |
| | | dtype = [('serial', int), ('vehicle', 'U2'), ('time', int)] |
| | | |
| | | # 确保每个字典包含所有字段 |
| | | structured_list = [] |
| | | for row in dynamicMatrix: |
| | | for d in row: |
| | | # 提取字段,确保 'time' 存在,否则设置为默认值(例如 0.0) |
| | | serial = d.get('serial', 0) |
| | | vehicle = d.get('vehicle', '0') |
| | | time_val = d.get('time', 0) |
| | | structured_list.append((serial, vehicle, time_val)) |
| | | |
| | | # 将嵌套的列表转换为结构化数组 |
| | | structured_array = np.array([tuple(d.values()) for row in dynamicMatrix for d in row], dtype=dtype) |
| | | structured_array = np.array(structured_list, dtype=dtype) |
| | | # 重塑为原始的二维形状 |
| | | return structured_array.reshape(len(dynamicMatrix), -1) |
| | | |