I have a range of integers spanning from x to y, where x >= 0, y > x. I also have a step-size z, where z > 0, x % z == 0, y % z == 0.
I want a sequence of two-tuples like so:
(x, x + z)
(x + z, x + 2*z)
…
(x + n * z, y)
Is there a more elegant way to generate this sequence than:
[(i, j) for i, j in zip(range(x, y, z), range(x+z, y+z, z))]
The reason I’m looking for another way to spell this is that zip(range(x, y, z), range(x+z, y+z, z)) gets very long when using more descriptive variable names than x, y, z.
If I’m understanding you correctly, the two ranges here are identical except that the second range is precisely z higher - or, looking at it another way, you could take the combined range, then slice off the first and slice off the last to make the two ranges.
In that case, what I’d do would be:
span = range(x, y+z, z)
[(i, j) for i, j in zip(span[:-1], span[1:])]
All of the details of the breadth of the span are handled first, and then the pairing up happens afterwards.