Python:为什么要将数据插入list/di的两个dict中

2024-04-25 17:05:30 发布

您现在位置:Python中文网/ 问答频道 /正文

我知道这可能是一个非常幼稚的错误,但这已经让我紧张了很久。你知道吗

我有一个嵌套字典列表,比如

grs = [{
    'CS9': {
        'Monday': [{
                1: {
                    'subject': 'ESD',
                    'teacherName': 'Goku',
                    'venue': 'RN 141'
                }
            }, {
                3: {
                    'subject': 'CN',
                    'teacherName': 'vegita',
                    'venue': 'RN 102'
                }
            }, {
                5: {
                    'subject': 'ADA',
                    'teacherName': 'roshi',
                    'venue': 'RN 112'
                }
            }
        ]

    }
}, {
    'CS10': {
        'Monday': [{
                1: {
                    'subject': 'ESD',
                    'teacherName': 'Gohan ',
                    'venue': 'RN 141'
                }
            }, {
                3: {
                    'subject': 'CN',
                    'teacherName': 'Saitama',
                    'venue': 'RN 102'
                }
            }, {
                5: {
                    'subject': 'ADA',
                    'teacherName': 'Mob',
                    'venue': 'RN 112'
                }
            }
        ]
    }
}
]

简言之,两组以Monday为节点,不同的events位于不同的slots

所以,我想做的是更新CS10Monday节点 数据=>

tempStuff = [{'subject': 'ESD', 'teacherName': 'Aizen', 'venue': 'RN141'}, 'CS10', 2, 'Monday']

所以我要做的是:

for i in grs:
    if tempStuff[1] in i.keys():
        i[tempStuff[1]][tempStuff[3]].append({tempStuff[2]: tempStuff[0]})

我希望添加这个代码来添加另一个字典wiz

            {
                2: {
                    'subject': 'ESD',
                    'teacherName': 'Aizen',
                    'venue': 'RN141'
                }
            }

但它也会将这个节点添加到CS9。我尝试了不同的方法来生成数据,并以不同的方式进行了插入,但是这两个节点都发生了变化。你知道吗

为什么?
有什么帮助吗?你知道吗

编辑1

裁剪后的json中没有问题,即我删除了两个节点中TuesdayFriday的数据,以确保问题中json的可读性,因为其他节点在插入中无关紧要。所以我只提到Monday。你知道吗

编辑2

似乎问题出现在运行时创建grs时,我们在这里所做的是我们自己声明grs,而在我的代码中grs被“填充”,或者数据插入是由其他循环的条件发生的。但在一切都完成之后,grs和我们在这里声明的一样,没有变化。i、 e.插入后得到的grs和这里声明的grs完全相同。你知道吗

编辑3

因此,下面的代码将从另一个嵌套的dict/list(FinalDict)向我们神秘的“grs”中插入内容。你知道吗

      dataArrange = {}

      for g in groups:  # groups is just a list of groups eg CS10, CS9
            for i in FinalDict: # the data from which i'm putting the stuff to grs
                weekdet = []
                for b in FinalDict[i]['lectures']:
                    lectdetails = []
                    if b[0] == 'Remedial Class':
                        slot = b[1]['slot']
                    else:
                        slot = b[3]['slot']
                        subject = b[0]
                        teacherName = b[1]
                        venue = b[2]
                        lectdetails = {'subject': subject, 'teacherName': teacherName, 'venue': venue}

                    try:

                        if ShortCode[lectdetails['subject']] in ElectiveStuff:
                            continue

                    except IndexError:
                        continue

                    if not lectdetails:
                        weekdet.append({slot: "Remedial Class"})
                    else:
                        weekdet.append({slot: lectdetails})

                dataArrange.update({i: weekdet})
            grs.append({g: dataArrange})

        with open('stuff.txt', 'w') as g:  # here we wrote grs in a file which makes me sad though it solves the problem
            g.write(json.dumps(grs))

Tags: 数据infor节点rnsubjectslotmonday
1条回答
网友
1楼 · 发布于 2024-04-25 17:05:30

正如我所怀疑的,这是一个共同的参考问题。此处:

dataArrange = {}

for g in groups:
    # code here
    dataArrange.update({i: weekdet})
    grs.append({g: dataArrange})

您正在一次又一次地重复使用dataArrange,因此最终每个grs[x]{g}引用的都是同一个惟一的dict。您必须理解Python从不隐式地复制任何内容。你知道吗

解决方法非常简单:不要反复使用相同的dataArrangedict,而是每次创建一个新的dict。你知道吗

相关问题 更多 >

    热门问题