开发者

looping dictionaries of {tuple:NumPy.array}

i have a set of dictionaries k of the form {(i,j):NumPy.array} over which I want to loop the NumPy.arrays for a certain evaluation.

I made the dictionarries as follows:

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

so i can always change the set of data i want to evaluate in my bigger set of code by changeing the original list of strings. However, this means i have to call for my dictionaries as eval(k) for k in datarr.

As a result, the loop i want to do looks like this for the moment :

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})
开发者_如何学Python

but is there a much nicer way to write this ? I tried following, but this didn't 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')`

Thx in advance


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 all the 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 probably just:

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) < .01 to NaN .

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜