Home > OS >  Save generated random numbers with numpy in dictionary
Save generated random numbers with numpy in dictionary

Time:04-20

I have a code that generates numbers using NumPy uniform distribution, but I want to save them in a dictionary.

x = [1391096.378,
9187722.876,
516012.8602,
238575.7104,
228114.5731,
4685929.962,
675871.7576,
2371583.637,
368942.1523,
39899030.28,
2068330.11,
2663072.562,
587247.8119,
2077461.753,
6620623.744,
246850.7431,
313607.552,
1108662.4,
250.7123965,
39.30786207,
1975239.413,
]

t = 0
for i in x: 
        s = i/31 
        a = (s   0.2*s)
        b = (s - 0.2*s)
        dft = np.random.uniform(a,b,31)
        t = 1 

But I don't know how to do that.

CodePudding user response:

In this case, the keys of the dictionary will be integers from 0 to 20. If you'd like to have the value of x as a key, then you can substitute the t with the x, like this: stored[x] = dft.

Or you can use f-strings to be more creative with the dictionary's keys

t = 0
stored = {}

for i in x: 
    s = i/31 
    a = (s   0.2*s)
    b = (s - 0.2*s)
    dft = np.random.uniform(a,b,31)
    stored[t] = dft
    t  = 1 

  • Related