Python - Dictionary - Writing and reading correctly

Asked

Viewed 54 times

0

I’m having a problem reading the data from a dictionary I created to save data rebuilt by a function...

The dictionary is written whenever the value of Amp and t is rebuilt in loop col. At each loop of Row the dictionary is saved in a list.

Excerpt from the code where the dictionary is written:

def OptimalFilter(inNoiseMatrix, inSi, inGsig, inDgSig):
    AmpTime = Verif = dict()
    ListData = []
    for row in range(0,1000):
        for col in range(0,9):
            ...
            ...
            Amp = mm(aCoef.T,Si)
            t   = mm(bCoef.T,Si)/mm(aCoef.T,Si)
            AmpTime.update({'E_Cell_'+str(col+1): Amp, 't_Cell_'+str(col+1): t})
        ListData.append(AmpTime) 
return ListData

I checked the reconstruction by printing the values of Amp and t and are correct. The problem occurs when I am processing the data from the list that the function returns. When choosing a dictionary key and iterating in the lists the value is equal:

AmpTimeXTvalid = OptimalFilter(Noise[3000:], XTvalid, gSig[3000:], DgSig[3000:])

for i in range(20):
    print(AmpTimeXTvalid[i]['E_Cell_1'])
9975.71782251447
9975.71782251447
9975.71782251447
9975.71782251447
9975.71782251447
9975.71782251447
9975.71782251447
9975.71782251447
9975.71782251447
9975.71782251447
9975.71782251447
9975.71782251447
9975.71782251447
9975.71782251447

What I did wrong in writing/reading the dictionary?

1 answer

0

I believe the problem is in the line below:

AmpTime.update({'E_Cell_'+str(col+1): Amp, 't_Cell_'+str(col+1): t})

You can change to:

AmpTime['E_Cell_'+str(col+1)] = Amp
AmpTime['t_Cell_'+str(col+1)] = t

The reading of it will be a little different than what you are doing. But, I believe you have no problem with this.

I hope it helps.

  • Thanks for the suggestion... after a while I found the mistake. It is in the fact that I forgot to clean the dictionary before entering the loop col. After removing the location of the line AmpTime = Verif = dict() for before entering the loop col worked properly.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.