6

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)

Moslem Uddin
  • 61
  • 1
  • 4

2 Answers2

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