How can I remove zeroes from a linspace list?

93 views Asked by At

My list is this:

np.linspace(-20,20)

I want to get everything except for the zeroes.

I tried this:

x = list(map(int, np.linspace(-20,20)))
x.remove(0)

Also this:

[int(x) for x in np.linspace(-20,20) if x!=0]

But none of it worked.

4

There are 4 answers

0
Andrej Kesely On

IIUC, you can try:

arr = np.linspace(-20, 20, dtype=int)
print(arr[arr != 0])

Prints:

[-20 -20 -19 -18 -17 -16 -16 -15 -14 -13 -12 -12 -11 -10  -9  -8  -7  -7
  -6  -5  -4  -3  -3  -2  -1   1   2   2   3   4   5   6   6   7   8   9
  10  11  11  12  13  14  15  15  16  17  18  19  20]

Or in plain Python:

arr = [i for i in range(-20, 21) if i != 0]
1
Bashen On

If you want to get a numpy array without the zeroes, you can do:

L = numpy.linspace(-20, 20)
L = L[L != 0]

L != 0 creates an array of boolean that serves as a mask and L[L != 0] keeps the values where the mask is True.

However the output of numpy.linspace(-20, 20) does not have 0 in it.

2
Talha Tayyab On

np.linspace(-20,20) wont have zeroes in it.

print(np.linspace(-20,20))

array([-20.        , -19.18367347, -18.36734694, -17.55102041,
       -16.73469388, -15.91836735, -15.10204082, -14.28571429,
       -13.46938776, -12.65306122, -11.83673469, -11.02040816,
       -10.20408163,  -9.3877551 ,  -8.57142857,  -7.75510204,
        -6.93877551,  -6.12244898,  -5.30612245,  -4.48979592,
        -3.67346939,  -2.85714286,  -2.04081633,  -1.2244898 ,
        -0.40816327,   0.40816327,   1.2244898 ,   2.04081633,
         2.85714286,   3.67346939,   4.48979592,   5.30612245,
         6.12244898,   6.93877551,   7.75510204,   8.57142857,
         9.3877551 ,  10.20408163,  11.02040816,  11.83673469,
        12.65306122,  13.46938776,  14.28571429,  15.10204082,
        15.91836735,  16.73469388,  17.55102041,  18.36734694,
        19.18367347,  20.        ])

If you want values from -20 to 20 without 0 you can simply do with a list comprehension:

[x for x in range(-20,20) if x!=0]

np.linspace(-20,20) Returns evenly spaced numbers over a specified interval. These will be 50 values.

Number of samples to generate. Default is 50. Must be non-negative.

https://numpy.org/doc/stable/reference/generated/numpy.linspace.html

0
hpaulj On

I'm going to illustrate my comment, to give a clearer idea of what's happening with your remove

Make a smaller linspace, with only 10 elements (still even number, just not the default 50):

In [105]: x=np.linspace(-5,5,10);x
Out[105]: 
array([-5.        , -3.88888889, -2.77777778, -1.66666667, -0.55555556,
        0.55555556,  1.66666667,  2.77777778,  3.88888889,  5.        ])

Since it includes the end points an even number of elements can give fractional spacings.

Convert that to int (same as your list/map/int)

In [106]: x.astype(int)
Out[106]: array([-5, -3, -2, -1,  0,  0,  1,  2,  3,  5])

Note the 0s.

In [107]: y=x.astype(int).tolist()    
In [108]: y.remove(0)    
In [109]: y
Out[109]: [-5, -3, -2, -1, 0, 1, 2, 3, 5]    
In [110]: y.remove(0)    
In [111]: y
Out[111]: [-5, -3, -2, -1, 1, 2, 3, 5]

We have to call remove twice to get rid of both.

Repeat, but with an odd number:

In [112]: x=np.linspace(-5,5,11);x
Out[112]: array([-5., -4., -3., -2., -1.,  0.,  1.,  2.,  3.,  4.,  5.])

In [113]: x.astype(int)
Out[113]: array([-5, -4, -3, -2, -1,  0,  1,  2,  3,  4,  5])

In [114]: y=x.astype(int).tolist()    
In [115]: y.remove(0)

In [116]: y
Out[116]: [-5, -4, -3, -2, -1, 1, 2, 3, 4, 5]

Now there's only one 0, and remove takes care off it.

Whether these numbers are what you really need or not, this explains why remove didn't remove (all) the zeros.