Python 如何从嵌套循环中 return 嵌套字典?

Python how to return nested dictionary from nested loops?

我是 Python 和一般编程的新手,所以请耐心等待我和我的代码:)

我试图为我的一些机器收集一些基于班次的日期,但遇到了正确构建我的 return 字典的问题。

当我逐步执行该函数时,我可以看到每个班次都正确读取了数据,并且“maschinen_liste”指令和生成的“day”指令都是正确的,但是一旦我到达第二天,前一天条目中的数据更改为添加的当前数据,依此类推。 现在我的问题是字典中的条目如何像这样向后改变? 还是我根本不明白听写和嵌套循环是如何工作的?

我已经将代码简化到这一点,我仍然没有看到任何错误,而且我不能在我的生活中弄清楚这里出了什么问题。

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)

输出:

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}}}}}}}}

如果我将 day[temp_day] = maschinen_liste 更改为 day[temp_day] = str(maschinen_liste)+str(temp_day) 它正确地保存了日期...

输出:

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"}}}

好的,它是一个字符串,每个字符串都有一个数字,但这表明数据显然在那里(我很困惑...)

在您的代码中,您在所有循环之外的开头初始化 maschinen_liste = {"Pr5":{}}。当您执行 day[temp_day] = maschinen_liste 时,不会创建新词典(它只是对同一词典的引用)。所以最后,你每天都指向同一个字典。

如果将 maschinen_liste = {"Pr5":{}} 放入日循环中,在 for this_day in range(1,dayspermonth+1): 之后,应该可以解决问题,因为它会为每一天创建一个新字典。