replicate the seq function in R to python

Viewed 907

I am trying replicate R's seq function in Python

For example in R:

sequence = seq(from = 1, to = 3, by = 1)
output = 1 2 3

And in Python I find the linspace commmand:

np.linspace(start=1, stop=3, num=1)
output = array([1.])

But it specifies the number of elements rather than the step size.

I was looking for something like R's output.

2 Answers

Note that num is not equivalent to by.

num: int, optional

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


Try with

>>> np.linspace(start=1, stop=3, num=3)
array([1., 2., 3.])
[x for x in range(1, 4, 1)]
# [1, 2, 3]
Related