Thomas - 4 months ago 34

Python Question

I have problem during low-pass interpolation, I have to fill 1D array with zeros before processing it.

I have something like this: [1 2 3 4 5 6]

I want to have array like this [1 0 2 0 3 0 4 0 5 0 6] so it is L-1 zeros in array where L is the number of all values inside array before zero stuffing.

How to do it in Python?

Answer

You can assign an unpadded list of values into a slice of another list of zeros:

```
original_list = range(1,7) # [1,2,3,4,5,6]
padded_list = [0]*(2*len(original_list)-1) # [0,0,0,0,0,0,0,0,0,0,0]
padded_list[::2] = original_list # [1,0,2,0,3,0,4,0,5,0,6]
```

This can translate to `numpy`

too, though as Jaime pointed out in a comment, it's even easier to use `numpy.insert`

:

```
import numpy as np
arr = np.arange(1,7) # array([1, 2, 3, 4, 5, 6])
np.insert(arr, slice(1,None,2), 0) # array([1, 0, 2, 0, 3, 0, 4, 0, 5, 0, 6])
```