You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何合并一维与二维numpy数组并按指定格式存储?

无需修改维度存储Numpy数组为指定表格格式

需求说明

希望将Numpy数据存储为如下表格格式:

|   Column_A   |           Column_B                   |             Column_C                 | 
| 0.91512748   | [1.22773944 1.34972084  ...   0. ]   | [1.22773944 1.34972084  ...   0. ]   |
| 1.09939718   | [0.36013158 1.0634674   ...   0. ]   | [1.22773944 1.34972084  ...   0. ]   |
| 1.10390337   | [0.70320446 0.82676872   ...   0. ]   | [1.22773944 1.34972084  ...   0. ]   |
|      ...     |             ...                      |                  ...                 |

实际数据维度:

  • Column_A:(8741,) 一维Numpy数组
  • Column_B:(8741,60) 二维Numpy数组
  • Column_C:(8741,60) 二维Numpy数组

要求不修改原数据维度,实现目标存储格式。

解决方案

核心思路:直接将二维数组的每一行转换为目标格式的字符串,与一维数组元素配对构造表格行,全程不改变原数组的维度结构。

方法1:生成Markdown格式表格并保存

import numpy as np

# 替换为你的实际数据
column_a = np.random.rand(8741)
column_b = np.random.rand(8741, 60)
column_c = np.random.rand(8741, 60)

# 将二维数组行转换为带省略号的目标格式字符串
def array_to_str(arr, max_show=2):
    head_elements = [f"{x:.8f}" for x in arr[:max_show]]
    return f"[{(' '.join(head_elements))}  ...   0. ]"

# 构造表格内容
table_content = [
    "|   Column_A   |           Column_B                   |             Column_C                 |",
    "|--------------|--------------------------------------|--------------------------------------|"  # Markdown表格分隔线
]

# 遍历每一组数据生成行
for a_val, b_row, c_row in zip(column_a, column_b, column_c):
    a_str = f"{a_val:.8f}"
    b_str = array_to_str(b_row)
    c_str = array_to_str(c_row)
    table_content.append(f"| {a_str:>12} | {b_str:^36} | {c_str:^36} |")

# 保存为Markdown文件
with open("numpy_table.md", "w", encoding="utf-8") as f:
    f.write("\n".join(table_content))

方法2:存储为纯文本表格(与示例格式完全匹配)

import numpy as np

# 替换为你的实际数据
column_a = np.random.rand(8741)
column_b = np.random.rand(8741, 60)
column_c = np.random.rand(8741, 60)

def array_to_str(arr, max_show=2):
    head_elements = [f"{x:.8f}" for x in arr[:max_show]]
    return f"[{(' '.join(head_elements))}  ...   0. ]"

# 构造文本内容
text_content = [
    "|   Column_A   |           Column_B                   |             Column_C                 |"
]

# 生成数据行
for a_val, b_row, c_row in zip(column_a, column_b, column_c):
    a_str = f"{a_val:.8f}"
    b_str = array_to_str(b_row)
    c_str = array_to_str(c_row)
    text_content.append(f"| {a_str:>12} | {b_str:<36} | {c_str:<36} |")

# 插入省略号行(可选,适用于数据量过大时)
if len(text_content) > 10:
    text_content.insert(5, "|      ...     |             ...                      |                  ...                 |")
    text_content.insert(6, "|      ...     |             ...                      |                  ...                 |")

# 保存为文本文件
with open("numpy_table.txt", "w", encoding="utf-8") as f:
    f.write("\n".join(text_content))

自定义调整说明

  • 修改array_to_str函数中的max_show参数,可以调整每行显示的数组元素数量;修改:.8f可以调整小数位数。
  • 如果不需要省略号,直接将函数改为返回str(arr)即可生成完整数组字符串。
  • 格式对齐通过字符串格式化的>, <, ^参数控制,可根据需求调整列宽。

内容的提问来源于stack exchange,提问作者Akash Parmar

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.12 02:06:18