Search code examples
pythondictionarynested-loops

Python how to return nested dictionary from nested loops?


I am new to Python and programming in general so please bear with me and my code :)

I was trying to collect some shift based date for some of my machines and came across the problem of properly building my return dictionary.

when i step through the function i can see that the data is read correctly every shift and the "maschinen_liste" dict is correct as well as the resulting "day" dict but as soon as i get to the next day the data inside the previous day entry changes to the current data which is added aswell and so on. now to my question how can the entrys inside the dictionary change backwards like this ? Or do i just fundamentally don't understand how dicts and nested loops work ?

I already simplified the code to this point at which i still don't see anything wrong and i can't for the live of me figure out what is wrong here.

import random

NrByMonth = {
                "Januar": "01" ,
                "Februar": "02"
            }


def Readshiftdata(year_input="2020"):
    # dict structured like  dataframe = {"Jahr":{"Monat":{"Tag":{"Presse":{"Schicht":{"elektrisch":{"Verbrauch1":"","VerbrauchN":""},"informationen":{"Datum":"","Schichtführer":"","Nutzungsgrad":""},"produktionsdaten":{"Artikel1":{"Charge":"","Gutteile":"","Ausschuss1":"","Ausschuss2":"","Ausschuss3":"","Werker":"","Qualitaetspruefer":""},"Artikel2":{"Charge":"","Gutteile":"","Ausschuss1":"","Ausschuss2":"","Ausschuss3":"","Werker":"","Qualitaetspruefer":""},"Artikel3":{"Charge":"","Gutteile":"","Ausschuss1":"","Ausschuss2":"","Ausschuss3":"","Werker":"","Qualitaetspruefer":""},"Artikel4":{"Charge":"","Gutteile":"","Ausschuss1":"","Ausschuss2":"","Ausschuss3":"","Werker":"","Qualitaetspruefer":""}}}}}}}}
    return_data = {}
    month = {}
    year = year_input
    maschinen_liste = {"Pr5":{}}

    
    
    # iterating over every month
    for this_month in NrByMonth:

        day = {}
        # iterating over every day per month
        dayspermonth = 2
        this_day = 1
        for this_day in range(1,dayspermonth+1):   
            temp_day = this_day

            # reading per maschine
            for maschine in maschinen_liste:
            
                # reading shift per maschine
                shifts = {"Frühschicht":{},"Spätschicht":{},"Nachtschicht":{}}
                for shift in shifts:

                    this_shift = {"E-Daten":{},"Informationen":{},"Produktionsdaten":{}}
                    # fetching artikel data
                    artikel = {"Artikel 1":{}}

                    for a in artikel:

                        dataframe = {}
                        Charge = random.randint(1,100)
                        Gutteile = random.randint(1,100)
                        dataframe = {"Charge":Charge,"Gutteile":Gutteile}

                        artikel[a] = dataframe

                    shifts[shift]["Produktionsdaten"] = artikel

                maschinen_liste[maschine] = shifts
                print(maschinen_liste)
        


            day[temp_day] = maschinen_liste
            print("temp day :")
            print(temp_day)
            print("day:")
            print(day)
            print("maschinen_liste :")
            print(maschinen_liste)
            input()

        month[this_month] = day
        print("month:\n\n\n")
        print(month)


    return_data[year] = month

    return(return_data)

year = Readshiftdata()

print("year: \n\n\n")
print(year)

Output:

year = {'2020': {
    'Januar': {
        1: {
            'Pr5': {
                'Frühschicht': {'Produktionsdaten': {'Artikel 1': {'Charge': 41, 'Gutteile': 95}}}, 
                'Spätschicht': {'Produktionsdaten': {'Artikel 1': {'Charge': 37, 'Gutteile': 54}}}, 
                'Nachtschicht': {'Produktionsdaten': {'Artikel 1': {'Charge': 37, 'Gutteile': 39}}}}}, 
                
        2: {
            'Pr5': {
                'Frühschicht': {'Produktionsdaten': {'Artikel 1': {'Charge': 41, 'Gutteile': 95}}}, 
                'Spätschicht': {'Produktionsdaten': {'Artikel 1': {'Charge': 37, 'Gutteile': 54}}}, 
                'Nachtschicht': {'Produktionsdaten': {'Artikel 1': {'Charge': 37, 'Gutteile': 39}}}}}}, 
                
    'Februar': {
        1: {
            'Pr5': {
                'Frühschicht': {'Produktionsdaten': {'Artikel 1': {'Charge': 41, 'Gutteile': 95}}}, 
                'Spätschicht': {'Produktionsdaten': {'Artikel 1': {'Charge': 37, 'Gutteile': 54}}}, 
                'Nachtschicht': {'Produktionsdaten': {'Artikel 1': {'Charge': 37, 'Gutteile': 39}}}}}, 
                
        2: {
            'Pr5': {
                'Frühschicht': {'Produktionsdaten': {'Artikel 1': {'Charge': 41, 'Gutteile': 95}}}, 
                'Spätschicht': {'Produktionsdaten': {'Artikel 1': {'Charge': 37, 'Gutteile': 54}}}, 
                'Nachtschicht': {'Produktionsdaten': {'Artikel 1': {'Charge': 37, 'Gutteile': 39}}}}}}}}

If i change day[temp_day] = maschinen_liste to day[temp_day] = str(maschinen_liste)+str(temp_day) it saves the date correctly...

Output:

year = {'2020': {
    'Januar': {
        1: "{
            'Pr5': {'Frühschicht': {'Produktionsdaten': {'Artikel 1': {'Charge': 77, 'Gutteile': 44}}}, 
            'Spätschicht': {'Produktionsdaten': {'Artikel 1': {'Charge': 100, 'Gutteile': 72}}}, 
            'Nachtschicht': {'Produktionsdaten': {'Artikel 1': {'Charge': 93, 'Gutteile': 22}}}}}1", 
            
        2: "{
            'Pr5': {
                'Frühschicht': {'Produktionsdaten': {'Artikel 1': {'Charge': 27, 'Gutteile': 21}}}, 
                'Spätschicht': {'Produktionsdaten': {'Artikel 1': {'Charge':27, 'Gutteile': 29}}}, 
                'Nachtschicht': {'Produktionsdaten': {'Artikel 1': {'Charge': 41, 'Gutteile': 72}}}}}2"}, 
                
    'Februar': {
        1: "{
            'Pr5': {
                'Frühschicht': {'Produktionsdaten': {'Artikel 1': {'Charge': 74, 'Gutteile': 27}}}, 
                'Spätschicht': {'Produktionsdaten': {'Artikel 1': {'Charge': 65, 'Gutteile': 83}}}, 
                'Nachtschicht': {'Produktionsdaten': {'Artikel 1': {'Charge': 14, 'Gutteile': 11}}}}}1", 
                
        2: "{
            'Pr5': {'Frühschicht': {'Produktionsdaten': {'Artikel 1': {'Charge': 19, 'Gutteile': 16}}}, 
            'Spätschicht': {'Produktionsdaten': {'Artikel 1': {'Charge': 57, 'Gutteile': 17}}}, 
            'Nachtschicht': {'Produktionsdaten': {'Artikel 1': {'Charge': 56, 'Gutteile': 52}}}}}2"}}}

ok its a string and there is a number addes to every string but this shows that the data is obviously there (i am confused...)


Solution

  • In your code, you initialize maschinen_liste = {"Pr5":{}} at the beginning, outside all of your loops. When you do day[temp_day] = maschinen_liste, no new dictionary is created (it's only a reference to the same dictionary). So in the end, you end up with each day pointing at the same dictionary.

    If you put maschinen_liste = {"Pr5":{}} inside your day loop, after for this_day in range(1,dayspermonth+1):, that should solve the problem, since it will create a new dictionary for every day.