2

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
  • Duplicate of stackoverflow.com/q/18265935/680068 ? Commented Feb 8, 2021 at 10:06
  • I understand that these kinds of questions would be duplicates almost always, but I would appreciate the community to be understanding of the following situation: people like me, learning Python after R tend to ask questions exactly like the OP "What is the equivalent of X function in Python?". These are legit and normal questions given that one learns how to navigate the help documentation and "philosophy" of a new language. I am not yet aware of a place where such questions would be fully welcomed without feeling that we saturate StackOverflow with duplicated questions. Commented May 17, 2022 at 6:40

2 Answers 2

1

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.])
Sign up to request clarification or add additional context in comments.

4 Comments

so isnt there a replicate of by in python ?
np.arange(11, 17, 0.5).tolist()
@pylearner Well, seq equivalent is np.linspace but what I am saying is that num defines the number of samples you want to generate (in this case 3) but in seq, by argument is actually the step.
oh i got it ... thanks @giorgos
0
[x for x in range(1, 4, 1)]
# [1, 2, 3]

1 Comment

np.arange(11, 17, 0.5).tolist()

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.