我有一个数组[ 0 10 15 20 10 0 35 25 15 35 0 30 20 25 30 0]
,我需要在增量为5的位置插入另一个数组“[5,7,8,15]”的每个元素,以便最终数组看起来[ 0 10 15 20 5 10 0 35 25 7 15 35 0 30 8 20 25 30 0 15] length is 20
我正在尝试使用此代码
arr_fla = [ 0 10 15 20 10 0 35 25 15 35 0 30 20 25 30 0]
arr_split = [5,7,8,15]
node = 5
node_len = node * (node-1)
for w in range(node, node_len, 5):
for v in arr_split:
arr_fla = np.insert(arr_fla,w,v)
print(arr_fla)
我得到的结果是
'[ 0 10 15 20 10 15 8 7 5 0 15 8 7 5 35 15 8 7 5 25 15 35 0 30
20 25 30 0]' length 28
谁能告诉我哪里出了问题
如果大小像示例中一样整齐排列,则可以使用
reshape
。。。将
arr_split
追加为新列。。。然后再变平
我已经纠正了它:
在代码中:
这将一次获取所有元素(总共
w
次),但一次只需要一个元素。因此,您不需要额外的for
循环您希望在每次从第二个数组
arr_split
插入项时,计数器都会不断上升试试这个代码。我的假设是,可以直接插入最后一个元素,因为原始数组只有16个元素
输出:
相关问题 更多 >
编程相关推荐