Python Forum

Full Version: Dictionary with ranges that have a float step value
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hello to all!

I need to create dictionary like this:
rcd_curr = {'SOUR:SAF:RCDC': {0.025: (?????), 0.25: (???????), 2.5: (???????)}}
for key 0.025 I need generate values from 0.020 with step 0.0001
for key 0.25 I need generate values from 0.21 with step 0.001
for key 2.5 I need generate values from 2.1 with step 0.01
can anyone help with?
you could try using numpy.

import numpy as np
r1 = np.arange(0.020, 0.025, 0.0001)
for i in r1:
    print(i)
Output:
0.02 0.0201 0.0202 0.0203 0.020399999999999998 0.020499999999999997 0.020599999999999997 0.020699999999999996 0.020799999999999996 0.020899999999999995 0.020999999999999994 0.021099999999999994 0.021199999999999993 0.021299999999999993 0.021399999999999992 0.02149999999999999 0.02159999999999999 0.02169999999999999 0.02179999999999999 0.02189999999999999 0.02199999999999999 0.022099999999999988 0.022199999999999987 0.022299999999999986 0.022399999999999986 0.022499999999999985 0.022599999999999985 0.022699999999999984 0.022799999999999983 0.022899999999999983 0.022999999999999982 0.02309999999999998 0.02319999999999998 0.02329999999999998 0.02339999999999998 0.02349999999999998 0.02359999999999998 0.023699999999999978 0.023799999999999977 0.023899999999999977 0.023999999999999976 0.024099999999999976 0.024199999999999975 0.024299999999999974 0.024399999999999974 0.024499999999999973 0.024599999999999973 0.024699999999999972 0.02479999999999997 0.02489999999999997 0.02499999999999997
import pprint

group1 = []
group2 = []
group3 = []

for value in range(200, 251, 1):
    group1.append(round(value*0.0001, 4))
    if value >= 210:
        group2.append(round(value*0.001, 3))
        group3.append(round(value*0.01, 2))


rcd_curr = {'SOUR:SAF:RCDC': {0.025: tuple(
    group1), 0.25: tuple(group2), 2.5: tuple(group3)}}

pprint.pprint(rcd_curr)
Output:
{'SOUR:SAF:RCDC': {0.025: (0.02, 0.0201, 0.0202, 0.0203, 0.0204, 0.0205, 0.0206, 0.0207, 0.0208, 0.0209, 0.021, 0.0211, 0.0212, 0.0213, 0.0214, 0.0215, 0.0216, 0.0217, 0.0218, 0.0219, 0.022, 0.0221, 0.0222, 0.0223, 0.0224, 0.0225, 0.0226, 0.0227, 0.0228, 0.0229, 0.023, 0.0231, 0.0232, 0.0233, 0.0234, 0.0235, 0.0236, 0.0237, 0.0238, 0.0239, 0.024, 0.0241, 0.0242, 0.0243, 0.0244, 0.0245, 0.0246, 0.0247, 0.0248, 0.0249, 0.025), 0.25: (0.21, 0.211, 0.212, 0.213, 0.214, 0.215, 0.216, 0.217, 0.218, 0.219, 0.22, 0.221, 0.222, 0.223, 0.224, 0.225, 0.226, 0.227, 0.228, 0.229, 0.23, 0.231, 0.232, 0.233, 0.234, 0.235, 0.236, 0.237, 0.238, 0.239, 0.24, 0.241, 0.242, 0.243, 0.244, 0.245, 0.246, 0.247, 0.248, 0.249, 0.25), 2.5: (2.1, 2.11, 2.12, 2.13, 2.14, 2.15, 2.16, 2.17, 2.18, 2.19, 2.2, 2.21, 2.22, 2.23, 2.24, 2.25, 2.26, 2.27, 2.28, 2.29, 2.3, 2.31, 2.32, 2.33, 2.34, 2.35, 2.36, 2.37, 2.38, 2.39, 2.4, 2.41, 2.42, 2.43, 2.44, 2.45, 2.46, 2.47, 2.48, 2.49, 2.5)}}