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

如何对numpy数组列表按反对角线规则求和得到预期输出

计算实现方法

你描述的规则本质是求数组堆叠后二维矩阵的反对角线元素和,直接用numpy即可快速实现。

可直接运行的代码

import numpy as np

# 输入数组列表
arr_list = [
    np.array([59, 24,  9,  3,  1,  0,  0,  0,  0,  0,  0,  0], dtype=np.int64),
    np.array([118,  48,  19,   7,   3,   1,   0,   0,   0,   0,   0,   0], dtype=np.int64),
    np.array([178,  72,  29,  11,   4,   1,   0,   0,   0,   0,   0,   0], dtype=np.int64),
    np.array([237,  96,  39,  15,   6,   2,   1,   0,   0,   0,   0,   0], dtype=np.int64),
    np.array([296, 120,  49,  19,   8,   3,   1,   0,   0,   0,   0,   0], dtype=np.int64),
    np.array([356, 144,  58,  23,   9,   3,   1,   0,   0,   0,   0,   0], dtype=np.int64),
    np.array([415, 168,  68,  27,  11,   4,   1,   0,   0,   0,   0,   0], dtype=np.int64),
    np.array([474, 193,  78,  31,  12,   5,   2,   0,   0,   0,   0,   0], dtype=np.int64),
    np.array([534, 217,  88,  35,  14,   5,   2,   0,   0,   0,   0,   0], dtype=np.int64),
    np.array([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], dtype=np.int64),
    np.array([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], dtype=np.int64),
    np.array([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], dtype=np.int64)
]

# 堆叠为二维矩阵,按反对角线求和
mat = np.vstack(arr_list)
max_offset = mat.shape[0]
result = [int(np.trace(mat, offset=-s)) for s in range(max_offset * 2 - 1)]

完整输出结果

全量输出(含末尾连续0):
[59, 142, 235, 331, 429, 526, 622, 717, 812, 751, 306, 126, 50, 21, 8, 2, 0, 0, 0, 0, 0, 0, 0]

非零有效输出序列:

  • 59
  • 142
  • 235
  • 331
  • 429
  • 526
  • 622
  • 717
  • 812
  • 751
  • 306
  • 126
  • 50
  • 21
  • 8
  • 2

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.11 09:24:02