I want to exclude starting point from a array in python using numpy. How I can execute? For example I want to exclude 0, but want to continue from the very next real number(i.e want to run from greater than 0) following code x=np.linspace(0,2,10)
Asked
Active
Viewed 8,196 times
6

Moslem Uddin
- 61
- 1
- 4
-
Please, share your codes and errors here. Also, explain more what you want. – Hamed Baziyad Jun 26 '20 at 12:19
-
To exclude 0 use `np.linspace(0,2,11)[1:]`. To exclude both 0 and 2 use `np.linspace(0,2,12)[1:-1]`. – Michael H. Jan 26 '22 at 22:35
-
A supplement: `np.linspace(2, 0, 10, endpoint=False)` – Sean.H Dec 05 '22 at 10:20
2 Answers
5
x=np.linspace(0,2,10)[1:] #remove the first element by indexing
print(x)
[0.22222222 0.44444444 0.66666667 0.88888889 1.11111111 1.33333333
1.55555556 1.77777778 2. ]

Bernad Peter
- 504
- 5
- 12
4
Kind of an old question but I thought I'll share my solution to the problem.
Assuming you want to get an array
[0.2, 0.4, 0.6, 0.8, 1.0, 1.2, 1.4, 1.6, 1.8, 2.]
you can make use of the endpoint option in np.linspace() and reverse the direction:
x = np.linspace(2, 0, 10, endpoint=False)[::-1]
the [::-1]
reverses the array so that it ends up being in the desired sequence.

HaddiWammi
- 41
- 3