The function range
(or arange
in NumPy) allows you to specify the inicio
, the end and the paso
, but if instead of knowing the end you know the number n
of elements the list must have, how could you create it?
Something equivalent to doing in R:
vector = inicio + paso * (0:n)
You can also opt for the
arange()
numpy functionHowever the question is Can it be done as long as the step
pero si en vez de conocer el final sabes el número de elementos que ha de tener la lista, ¿cómo podrías crearla?
is specifiedAnd you just run the function
and the result will be
I
numElemt
subtract 1 since at the beginning the first element was added, if you want you can omit it-1
but the difference with arange would be that it does not return the number if it is greater than the one specified instop
and you will have one more element than the specifiedIn my opinion, the most native way to do this in Python is to use the following elements:
itertools.count
: Which is an iterator whose initialization method receives two values:start
andstep
, plus its method__next__
does not have a stop criterion.List comprehension: To create the output list, based on a
range()
determining its length, and usingnext()
iterator values to invokecount
.The code would be (Given a start at 1.5 , a step of 0.5 and an output list length of 10 elements ):
output
will contain:With
Python
I can think of a super basic implementation:And it would work like this:
However, I suggest you check out this code which appears to be a more rigorous implementation of the
seq()
R function