Reputation: 37487
I want to initialize an array with 10 values starting at X and incrementing by Y. I cannot directly use range()
as it requires to give the maximum value, not the number of values.
I can do this in a loop, as follows:
a = []
v = X
for i in range(10):
a.append(v)
v = v + Y
But I'm certain there's a cute python one liner to do this ...
Upvotes: 7
Views: 17965
Reputation: 63777
Just another way of doing it
Y=6
X=10
N=10
[y for x,y in zip(range(0,N),itertools.count(X,Y))]
[10, 16, 22, 28, 34, 40, 46, 52, 58, 64]
And yet another way
map(lambda (x,y):y,zip(range(0,N),itertools.count(10,Y)))
[10, 16, 22, 28, 34, 40, 46, 52, 58, 64]
And yet another way
import numpy
numpy.array(range(0,N))*Y+X
array([10, 16, 22, 28, 34, 40, 46, 52, 58, 64])
And even this
C=itertools.count(10,Y)
[C.next() for i in xrange(10)]
[10, 16, 22, 28, 34, 40, 46, 52, 58, 64]
Upvotes: 2
Reputation: 3519
If I understood your question correctly:
Y = 6
a = [x + Y for x in range(10)]
Edit: Oh, I see I misunderstood the question. Carry on.
Upvotes: 1
Reputation: 213115
You can use this:
>>> x = 3
>>> y = 4
>>> range(x, x+10*y, y)
[3, 7, 11, 15, 19, 23, 27, 31, 35, 39]
Upvotes: 9
Reputation: 76254
>>> x = 2
>>> y = 3
>>> [i*y + x for i in range(10)]
[2, 5, 8, 11, 14, 17, 20, 23, 26, 29]
Upvotes: 17