Home > database >  Loop that iterate lists in nested dictionaries
Loop that iterate lists in nested dictionaries

Time:03-11

I am trying to build a loop that iterate over a dictionary of dictionaries containing lists.

For each sub dictionaries inside the main dictionary (dict_all), I would like to iterate over all the lists, in order to subtract the first element of the list (column 'time' only) to each elements of the corresponding list. To give some context, here is how the structure of the nested dictionaries looks like:

dict_all = { 'dict_20' : {"P1S1": df_list_20[0], "P2S1": df_list_20[1], "P3S1": df_list_20[2], "P4S1": df_list_20[3], 
           "P5S1": df_list_20[4], "P6S1": df_list_20[5], "P7S1": df_list_20[6], "P8S1": df_list_20[7],
           "P9S1": df_list_20[8], "P10S1": df_list_20[9], "P1S2": df_list_20[10], "P2S2": df_list_20[11],
           "P3S2": df_list_20[12], "P4S2": df_list_20[13], "P5S2": df_list_20[14], "P6S2": df_list_20[15],
           "P7S2": df_list_20[16], "P8S2": df_list_20[17], "P9S2": df_list_20[18], "P10S2": df_list_20[19]},
           
           'dict_40' : {"P1S1": df_list_40[0], "P2S1": df_list_40[1], "P3S1": df_list_40[2], "P4S1": df_list_40[3], 
           "P5S1": df_list_40[4], "P6S1": df_list_40[5], "P7S1": df_list_40[6], "P8S1": df_list_40[7],
           "P9S1": df_list_40[8], "P10S1": df_list_40[9], "P1S2": df_list_40[10], "P2S2": df_list_40[11],
           "P3S2": df_list_40[12], "P4S2": df_list_40[13], "P5S2": df_list_40[14], "P6S2": df_list_40[15],
           "P7S2": df_list_40[16], "P8S2": df_list_40[17], "P9S2": df_list_40[18], "P10S2": df_list_40[19]}}

(I only took two nested dictionaries for the sake of example, but I have a total of six)

This is an example of how the lists inside the secondaries dictionaries looks like (each list have a different length):

        time  velocity
0   10.755616  0.059656
1   10.776415  0.131182
2   10.796519  0.232206
3   10.816632  0.355268
4   10.836832  0.531719
5   10.857092  0.751184
6   10.877205  0.964944
7   10.897274  1.136908
8   10.917387  1.333836
9   10.937439  1.553230
10  10.957535  1.726184
11  10.977587  1.870065
12  10.997622  2.049414
13  11.017821  2.211132
14  11.037908  2.248697
15  11.057977  2.261453
16  11.078099  2.244839
17  11.098168  2.116589
18  11.118351  1.981216
19  11.138455  1.774250
20  11.158568  1.499906
21  11.178724  1.250161
22  11.198863  1.002039
23  11.219028  0.751948
24  11.239314  0.486585
25  11.259696  0.264919

The whole purpose of the loop is for each lists, makes the 'time' column start at '0' while maintaining the same time intervals between each rows.

I managed to code the following loop that iterate over an isolated list, and that give me the desired output:

for i in range(len(dfList_20[0]['time'])): 
    dfList_20[0]['time'][:] = dfList_20[0]['time'][:] - dfList_20[0]['time'][0]

But of course it would not be very efficient to iterate at the level of the lists, as it would take me 240 lines of code to fully reach the desired output ..

Thank you all in advance for your time and your solutions ! :)

CodePudding user response:

Use nested loops

for nested_dict in dict_all.values():
    for dflist in nested_dict.values():
        dflist['time'][:] = dflist['time'][:] - dflist['time'][0]
  • Related