BenjaminK
BenjaminK

Reputation: 783

For loop adding dicts in dicts without overwriting

I'm trying to write the code as simple as possible. And it's already creating nearly the dict I'm looking for. But the subdictionaries {str(row[0]):are sadly going to be overwritten. Now I'm looking for a way to store the first values of a line for each Blue Item in subdict without overwriting the values before it.

Sample Data:

header = ["title 01", "title 02", "title 03", "title 04", "title 05", "title 06", "title 07", "title 08"]

table = [
    "Blue Item 01",
    "221 4.055 8.452581582427 4.4838340356 8.679611220757502 5.55358892234839 9.09286848348221896 9.45332056441802",
    "36500 9.488 3.48463473994 9.0407483482094 3.7868682447595 2.8868694465 7.0846841221896 10.4532222485381913",
    "*863840* 4.48 3.48684556114014 8.48383792859 6.638658464 4.255483845575 98.8348251221896 6.453140463508968",
    "864 83.45 8.48383838 8.4216448838433569 0.278373887378 4.4886384384 48.48384396 9.11435133645320674",

    "Blue Item 02",
    "3450 254.525 0.716826 0.1119312 0.774307631256817 2.66236555555556 0.0927734375 75.106087857865277778",
    "*486* 68786.0855 0.581674826764762 0.079744514631212 0.731168630226715 2.98093827813281 75.09277875775 0.107678128890664",
    "864 7858.5805 843.5248380816 9.04838432127 3.48348384830 3.1868738734687 0.043834884375 4838.483843734",

    "Blue Item 03",
    "9600 2.528463 0.84684626 486846.4868312 48648.7743048648625486817 8464.66234868465556 48.0948684375 8648.486847778",
    "*1023* 8.486845 48684.584868762 4868.0797846481212 48648.7314868715 2.980486843281 4864.4868734375 48684.148648890664",
    "8453 25.4525 9.5448684816 48648.066353806612787 486.7095378308819 846697.186078614687 78640.0927786784375 786.1086831734"]

Code:

table_dict = {}
for i in table:
    global blue_item
    if i.startswith("Bl"):
        blue_item = str(i)
        # print(i)
    else:
        # print(i)
        row = i.split(" ")
        table_dict[blue_item] = {                # Blue Item
            str(header[0]): {str(row[0]): {       # title 01
                str(header[1]): float(row[1]),    # title 02
                str(header[2]): float(row[2]),    # title 03
                str(header[3]): float(row[3]),    # title 04
                str(header[4]): float(row[4]),    # title 05
                str(header[5]): float(row[5]),    # title 06
                str(header[6]): float(row[6]),    # title 07
                str(header[7]): float(row[7])}}}  # title 08

pprint(table_dict)

Goal: (Only parts of the goal to get the idea)

 'Blue Item 01': {'title 01': {'221': {'title 02': 4.055,
                                                         'title 03': 8.452581582427,
                                                         'title 04': 4.4838340356,
                                                         'title 05': 8.679611220757502,
                                                         'title 06': 4.24622875213704,
                                                         'title 07': 5.55358892234839,
                                                         'title 08': 9.45332056441802}
                                              {'36500': {'title 01': 9.488 ,
                                                         'title 02': 3.48463473994,
                                                         'title 03': 9.0407483482094,
                                                         'title 04': 3.7868682447595,
                                                         'title 05': 2.8868694465,
                                                         'title 06': 7.0846841221896,
                                                         'title 07': 7.0846841221896,
                                                         'title 08': 10.4532222485381913}
                                            {'*863840*': {'title 01': 4.48 ,
......

}},

Upvotes: 0

Views: 56

Answers (1)

k.kolev
k.kolev

Reputation: 93

The problem was the overwriting of table_dict[blue_item] inside the else block. The solution below solves that problem.

table_dict = {}
blue_item = ''
for i in table:
    global blue_item
    if i.startswith("Bl"):
        blue_item= str(i)
        table_dict[blue_item] = {
            str(header[0]): {}
        }
        # print(i)
    else:
        # print(i)
        row = i.split(" ")
        table_dict[blue_item][str(header[0])][str(row[0])] = {
                str(header[1]): float(row[1]),
                str(header[2]): float(row[2]), 
                str(header[3]): float(row[3]),
                str(header[4]): float(row[4]),
                str(header[5]): float(row[5]),
                str(header[6]): float(row[6]),
                str(header[7]): float(row[7])} 

Upvotes: 1

Related Questions