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

Python中在数组指定位置外插入零的实现问题

问题:依据指定位置在数组非目标位置插入零

我有列表T2和numpy数组X,需要按以下规则处理:对X[0],在T2[0]指定的位置之外插入零;对X[1],在T2[1]指定的位置之外插入零。以下是我的代码、当前输出及期望输出,请帮忙修正代码得到正确结果。

我的代码

import numpy as np

T2=[[0, 3, 5, 8, 9, 10, 11],[0, 2, 3, 5, 6, 8, 9, 10, 11]]

X=np.array([np.array([4.17551036e+02, 3.53856161e+02, 2.82754301e+02, 1.34119055e+02,
              6.34573886e+01, 2.08344718e+02, 1.00000000e-24])               ,
       np.array([4.17551036e+02, 3.32821605e+02, 2.94983702e+02, 2.78809292e+02,
              1.26991664e+02, 1.36026510e+02, 8.31512525e+01, 2.07329562e+02,
              1.00000000e-24])                                               ],
      dtype=object)

C1=0.0

index=0

for m in range(0,len(X)):
    for j in range(T2[m][-1]):
        if(j!=T2[m][index]):
            X[m] = np.insert(X[m], j, C1, axis=None)  
        else:
            index+=1

print([X])

当前输出

[array([array([4.17551036e+02, 0.00000000e+00, 0.00000000e+00, 3.53856161e+02,
              0.00000000e+00, 2.82754301e+02, 0.00000000e+00, 0.00000000e+00,
              1.34119055e+02, 6.34573886e+01, 2.08344718e+02, 1.00000000e-24]),
       array([0.00000000e+00, 0.00000000e+00, 0.00000000e+00, 0.00000000e+00,
              0.00000000e+00, 0.00000000e+00, 0.00000000e+00, 0.00000000e+00,
              0.00000000e+00, 4.17551036e+02, 3.32821605e+02, 2.94983702e+02,
              2.78809292e+02, 1.26991664e+02, 1.36026510e+02, 8.31512525e+01,
              2.07329562e+02, 1.00000000e-24])                               ],
      dtype=object)]

期望输出

[array([array([4.17551036e+02, 0.00000000e+00, 0.00000000e+00, 3.53856161e+02,
              0.00000000e+00, 2.82754301e+02, 0.00000000e+00, 0.00000000e+00,
              1.34119055e+02, 6.34573886e+01, 2.08344718e+02, 1.00000000e-24]),
       array([4.17551036e+02, 0.00000000e+00, 3.32821605e+02, 2.94983702e+02,
              0.00000000e+00, 2.78809292e+02, 1.26991664e+02, 0.00000000e+00,
              1.36026510e+02, 8.31512525e+01, 2.07329562e+02, 1.00000000e-24]) ],
      dtype=object)]   

修正后的代码

原代码存在两个核心问题:一是index变量未在每个子数组处理时重置,导致处理第二个数组时索引混乱;二是循环插入元素会持续改变数组长度,引发后续索引匹配错误。更高效准确的方式是直接创建全零数组,再将原数组值映射到指定位置:

import numpy as np

T2=[[0, 3, 5, 8, 9, 10, 11],[0, 2, 3, 5, 6, 8, 9, 10, 11]]

X=np.array([np.array([4.17551036e+02, 3.53856161e+02, 2.82754301e+02, 1.34119055e+02,
              6.34573886e+01, 2.08344718e+02, 1.00000000e-24]),
       np.array([4.17551036e+02, 3.32821605e+02, 2.94983702e+02, 2.78809292e+02,
              1.26991664e+02, 1.36026510e+02, 8.31512525e+01, 2.07329562e+02,
              1.00000000e-24])], dtype=object)

# 逐个处理每个子数组
for m in range(len(X)):
    # 确定目标数组的长度(T2子列表最后一个位置+1)
    target_length = T2[m][-1] + 1
    # 创建对应长度的全零数组
    result_array = np.zeros(target_length, dtype=X[m].dtype)
    # 将原数组的值映射到指定位置
    result_array[T2[m]] = X[m]
    # 更新X中的子数组
    X[m] = result_array

print([X])

说明

  • 根据T2每个子列表的最后一个元素确定目标数组的总长度;
  • 用全零数组作为基础,通过索引直接赋值的方式将原数组元素放到指定位置,彻底避免循环插入带来的索引混乱;
  • 每个子数组独立处理,互不干扰,逻辑清晰且执行效率更高。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.06 07:10:27