Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create list of numbers mirrored around zero (python)

Tags:

python

list

numpy

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]

and

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!

edit: my own solution was

def mylist(stop,step):
    a = list(np.arange(0,-stop,-step))+[-stop]
    a.reverse()
    b = list(a)
    c = list(np.arange(0,stop,step))+[stop]
    d = b+c
    d.remove(0)
    e = list(d)
    return e

which is horribly clunky, even I can see.

The best answer was:

def mirrored(maxval, inc):
    x = np.arange(inc, maxval, inc)
    if x[-1] != maxval:
        x = np.r_[x, maxval]
    return np.r_[-x[::-1], 0, x]

but I am going to have to google a little more to understand why that works (also not sure if I want to round... the input for the increment might be legitimately specified to more than one decimal place)

like image 305
Tom Avatar asked Aug 10 '13 15:08

Tom


People also ask

How do you make a list filled with 0 in Python?

The easiest and most used method for creating a list with only zeros is the use of the steric “*” operator in the python code.

How do I make a list of sequential numbers in Python?

How do you create a sequence of numbers in Python? Use range() to generate a sequence of numbers Call range(start, stop) to generate a sequence of numbers from start up to stop . Use a for-loop to iterate over each number in this sequence and use list.

How do you make a list of numbers from 0 to 100 in Python?

Using the range() function to create a list from 1 to 100 in Python. In Python, we can use the range() function to create an iterator sequence between two endpoints. We can use this function to create a list from 1 to 100 in Python. The function accepts three parameters start , stop , and step .

How do I make a list of 100 numbers in Python?

Python Create List from 0 to 100. A special situation arises if you want to create a list from 0 to 100 (included). In this case, you simply use the list(range(0, 101)) function call. As stop argument, you use the number 101 because it's excluded from the final series.


1 Answers

If you want it to be strictly mirrored around 0, (i.e. always include 0 and the endpoints, and be perfectly symmetric about 0) you'll need a couple of steps.

First off, be aware of @NPE's comment above. Floating point math is not the same as decimal math!! This may seem beside the point, but it will bite you in certain circumstances.

There's more than one way to do this. Do you want to have all of the numbers be evenly spaced, or stick to the increment and only violate it at the endpoints?. This approach takes the latter of the two.

import numpy as np

def mirrored(maxval, inc=1):
    x = np.arange(inc, maxval, inc)
    if x[-1] != maxval:
        x = np.r_[x, maxval]
    return np.r_[-x[::-1], 0, x]

print mirrored(1, 0.3)

This yields:

[-1.  -0.9 -0.6 -0.3  0.   0.3  0.6  0.9  1. ]

If you want all of the numbers to be evenly spaced (but not the exact increment you specify), just use linspace:

import numpy as np

def mirrored2(maxval, inc=1):
    return np.linspace(-maxval, maxval, 2*maxval // inc)

print mirrored2(1, 0.3)

This yields:

[-1.  -0.6 -0.2  0.2  0.6  1. ]
like image 148
Joe Kington Avatar answered Oct 04 '22 23:10

Joe Kington