python numpy如何在每行之间插入多行

e1xvtsh3  于 2022-11-23  发布在  Python
关注(0)|答案(2)|浏览(149)

我有一个numpy数组,如下所示:

  • 26.4812 32.0000 -5.0000 10000 0000 20000.0000 2.0000
  • 26.4812 32.0000 10.0000 10000.0000 20000.0000 2.0000
  • 26.4812 32.0000 0.0000 10000 0.0000 20000 0.0000 2.0000......

我想更改它,使第3列(z值)具有更多的步骤,如下所示:

  • 26.4812 32.0000 -5.0000 10000 0000 20000.0000 2.0000
  • 26.4812 32.0000 -4.0000 10000 0000 20000.0000 2.0000
  • 26.4812 32.0000 -3.0000 10000 0000 20000.0000 2.0000
  • ...
  • 26.4812 32.0000 9.0000 10000 0000 20000 0000 2.0000
  • 26.4812 32.0000 10.0000 10000.0000 20000.0000 2.0000......这是一个很大的数字。

在本例中,步长必须由变量步长= 1 mm定义。如何实现?

djmepvbi

djmepvbi1#

这可能有点难看,但它确实符合您的要求:

arr1 = np.array([[1, -3, -3],
           [1, 3, 3],
           [1, 3, 7]])
z=arr1[:, 2]
new_z = []
for i in range(len(z)-1):
     new_z.append(np.arange(z[i],z[i+1]+1))
new_z = np.unique(np.concatenate(new_z))
new_array = np.c_[np.repeat(arr1[0, 0], new_z.shape[0]), new_z, 
np.repeat(arr1[0, 2], new_z.shape[0])]
print(new_array)
t1qtbnec

t1qtbnec2#

import numpy as np

def generate_array(zmin, zmax, step=1):
  # generate z values based on min, max and step (-5, 10, and 1 in the example)
  z_values= np.arange(zmin, zmax+step, step)

  # create an array with the same data for every z value (for now)
  default_value = 0
  array = np.repeat([[26.4812, 32.0000, default_value, 10000.0000, 20000.0000, 2.0000]], len(z_values), axis=0)
  
  # replace the z values
  array[:, 2] = z_values

  return array

可以这样使用

>>> generate_array(-5, 10)
>>> array([
  [ 2.64812e+01,  3.20000e+01, -5.00000e+00,  1.00000e+04, 2.00000e+04,  2.00000e+00],
  [ 2.64812e+01,  3.20000e+01, -4.00000e+00,  1.00000e+04, 2.00000e+04,  2.00000e+00],
  [ 2.64812e+01,  3.20000e+01, -3.00000e+00,  1.00000e+04, 2.00000e+04,  2.00000e+00],
  [ 2.64812e+01,  3.20000e+01, -2.00000e+00,  1.00000e+04, 2.00000e+04,  2.00000e+00],
  [ 2.64812e+01,  3.20000e+01, -1.00000e+00,  1.00000e+04, 2.00000e+04,  2.00000e+00],
  [ 2.64812e+01,  3.20000e+01,  0.00000e+00,  1.00000e+04, 2.00000e+04,  2.00000e+00],
  [ 2.64812e+01,  3.20000e+01,  1.00000e+00,  1.00000e+04, 2.00000e+04,  2.00000e+00],
  [ 2.64812e+01,  3.20000e+01,  2.00000e+00,  1.00000e+04, 2.00000e+04,  2.00000e+00],
  [ 2.64812e+01,  3.20000e+01,  3.00000e+00,  1.00000e+04, 2.00000e+04,  2.00000e+00],
  [ 2.64812e+01,  3.20000e+01,  4.00000e+00,  1.00000e+04, 2.00000e+04,  2.00000e+00],
  [ 2.64812e+01,  3.20000e+01,  5.00000e+00,  1.00000e+04, 2.00000e+04,  2.00000e+00],
  [ 2.64812e+01,  3.20000e+01,  6.00000e+00,  1.00000e+04, 2.00000e+04,  2.00000e+00],
  [ 2.64812e+01,  3.20000e+01,  7.00000e+00,  1.00000e+04, 2.00000e+04,  2.00000e+00],
  [ 2.64812e+01,  3.20000e+01,  8.00000e+00,  1.00000e+04, 2.00000e+04,  2.00000e+00],
  [ 2.64812e+01,  3.20000e+01,  9.00000e+00,  1.00000e+04, 2.00000e+04,  2.00000e+00],
  [ 2.64812e+01,  3.20000e+01,  1.00000e+01,  1.00000e+04, 2.00000e+04,  2.00000e+00]
])

相关问题