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.

Josiah Yoder
  • 3,321
  • 4
  • 40
  • 58
pylearner
  • 1,358
  • 2
  • 10
  • 26
  • Duplicate of https://stackoverflow.com/q/18265935/680068 ? – zx8754 Feb 08 '21 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. – Valentin_Ștefan May 17 '22 at 06:40

2 Answers2

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.])
Giorgos Myrianthous
  • 36,235
  • 20
  • 134
  • 156
0
[x for x in range(1, 4, 1)]
# [1, 2, 3]
s_baldur
  • 29,441
  • 4
  • 36
  • 69