Saturday, 10 August 2013

Create list of numbers mirrored around zero (python)

Create list of numbers mirrored around zero (python)

I think this should be a simple question... but it's been holding me up
for some time now :(
I wish to create a list of numbers, centred (as it were) on zero, from an
input that specifies the maximum and the increment. So,
max = 100 increment = 1 would return [-100,-99,-98,...,-1,0,1,...,99,100]
max = 35 increment = 0.2 would return
[-35.0,-34.8,...,-0.2,0,0.2,...34.8,35.0]
If the increment doesn't divide neatly into the maximum, it needs to make
a short last step (e.g. if counting to 1 in 0.3 increments, it would run
[-1.0,-0.6,-0.3,0.0,0.3,0.6,0.9,1.0]
list(numpy.linspace()) seems to be the way to go but I seem to be having a
complete mental block on how to make this work in the way described for
anything but the simplest cases.
Suggestions appreciated!

No comments:

Post a Comment