I am trying do write a quick simulation program. As a first step, I want to take an input and do the following:
29.0 in my example.27.5, 28.0, 28.5, 29.0, 29.5, 30, 30.5
Here is my program -- which is partially solving my problem.
#!/usr/bin/python
def getids(tk):
    tk=tk.lower()
        r=map(lambda x:x/10.0,range(0,6))   
    val={'PPP':29}
    val={k.lower():v for k,v in val.items()}
    if tk in val:
        stk_price=val[tk]   
    for i in r:
        stk_price=stk_price + i
        print(stk_price)
getids('PPP')
The output is
29.0
29.1
29.3
29.6
30.0
30.5
Is it possible to use numpy and generate values around a number ? Appreciate any help.
Use range() to create a list of numbers between two values. Call range(start, stop) to construct a sequence of numbers. The sequence ranges from start up to but not including stop . Use list() to convert this sequence to a list.
First a couple of notes on design:
You are probably looking for either np.arange or np.linspace. For your simple application, they are essentially equivalent. This is especially true because there is no roundoff error when stepping with an increment of 0.5.
For a given x, say 29, you could do either of the following:
step = 0.5
price_range = np.arange(x - step * 3, x + step * 4, step)
OR
step = 0.5
price_range = np.linspace(x - step * 3, x + step * 3, 7)
Personally I find linspace easier to understand here, because you just give it the limits and the number of points. For arange, you have to remember that the stop is exclusive, so you have to add a margin to x + step * 3.
I would recommend rephrasing your program like this:
stocks = {
    'PPP': 29,
}
# Notice that this only runs once instead of once per function call now
# casefold is the more generalized unicode version of lower
stocks = {key.casefold: value for key, value in stocks}
def getids(tk, margin=3, step=0.5):
    tk = tk.casefold()
    if tk in val:
        price = val[tk]
        return np.linspace(price - margin * step,
                           price + margin * step, 2 * margin + 1)
    return []
print(getids('PPP'))
If you want the exact output you had before, just join the elements with newlines:
print('\n'.join(map(str, getids('PPP'))))
or equivalently,
print('\n'.join(str(x) for x in getids('PPP')))
                        You can use arange function of numpy. 
Minimal example:
import numpy as np
num = 29    
np.arange(num-1.5, num+2, 0.5) # (start, stop, step)
The output will be
array([ 27.5,  28. ,  28.5,  29. ,  29.5,  30. ,  30.5])
                        Very similar to np. arange, with np.linspace, you can get an array of evenly spaced values, of whatever length you want. So, in your case, to get 3 values on the left and 3 values to the right (+1 to include your actual value), you'll want a total of 7 values. You can get them like this:
import numpy as np
x = 29
np.linspace(x-1.5, x+1.5, 7) #start, stop, number of values
# array([ 27.5,  28. ,  28.5,  29. ,  29.5,  30. ,  30.5])
                        Disclaimer: Not a numpy solution, but plain old python.
You can generate them using a combination of range / delta and steps around a piveau value:
def getSeries(piv, nums, delta):
    """Yields [piv + (-nums * delta):piv + nums * delta] in steps of delta"""
    for k in range(-nums,nums+1):
       yield piv + k*delta
for n in getSeries(29,3,0.5):
    print(f'{n:.1f}')
Output:
27.5  # -3
28.0  # -2
28.5  # -1
29.0  # piveau
29.5  #  1
30.0  #  2
30.5  #  3
                        If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With