Loop dictionaries {tuple: NumPy.array}

I have a set of kform dictionaries {(i,j):NumPy.array}over which I want to encode NumPy.arrays for a specific grade.

I made the dictionaries as follows:

datarr = ['PowUse', 'PowHea', 'PowSol', 'Top']  
for i in range(len(dat)): exec(datarr[i]+'={}')

therefore, I can always change the data set that I want to evaluate in my large code set by changing the original list of strings. However, this means that I have to call my dictionaries like eval(k) for k in datarr.

As a result, the loop I want to do looks something like this:

for i in filarr:  
    for j in buiarr:  
        for l in datarrdif:  
            a = eval(l)[(i, j)]  
            a[abs(a)<.01] = float('NaN')  
            eval(l).update({(i, j):a})

but is there a better way to write this? I tried following, but this did not work:

[eval(l)[(i, j)][abs(eval(l)[(i, j)])<.01 for i in filarr for j in buiarr for k in datarrdiff] = float('NaN')`

thanks in advance

+3
source share
1 answer
datarr = ['PowUse', 'PowHea', 'PowSol', 'Top']
for i in range(len(dat)): exec(datarr[i]+'={}')

Why don't you create them as a dictionary of dictionaries?

datarr = ['PowUse', 'PowHea', 'PowSol', 'Top']
data = dict((name, {}) for name in datarr)

Then you can avoid everyone eval().

for i in filarr:
    for j in buiarr:
        for l in datarr:
            a = data[l][(i, j)]
            np.putmask(a, np.abs(a)<.01, np.nan)
            data[l].update({(i, j):a})

or perhaps simply:

for arr in data.itervalues():
    np.putmask(arr, np.abs(arr)<.01, np.nan)

if you want to set all elements of all dictionary values, where abs(element) < .01is NaN.

+4
source

All Articles