在此列表中,为什么追加元素后重复?

弗洛里安·卡斯特莱恩

语境

我有一个包含字典的列表。我想“拉平”列表的所有字典(不带嵌套值的JSON)。为此,我编写了一个函数,该函数flatten_json可以按预期工作。

函数的每个输出都将附加到新列表,稍后将对其进行处理。

def flatten_json(cls, nested_dict, flattened_dict={}, superior_level_key: str = ""):
    for key, value in nested_dict.items():
        if type(nested_dict[key]) is dict:
            flattened_dict = cls.flatten_json(
                        nested_dict[key], flattened_dict, "{}_".format(key))
        else:
            flattened_dict['{}{}'.format(superior_level_key, key)] = value
    return flattened_dict

flatten_data = []
for accumulated_doc in accumulated_data:
    tmp = Utils.flatten_json(
               nested_dict=accumulated_doc['_source'])
    print(tmp)
    flatten_data.append(tmp)
print(flatten_data)

输出

{'typvoie': 'RUE', 'l3_normalisee': '', 'enseigne': '', 'libvoie': 'PASTEUR', 'nomen_long_text': '', 'apet700': '95.11Z', 'codpos_cp': '59260', 'siren': '479454803', 'l2_normalisee_text': '', 'nomen_long_initial': '', 'nic': '00038', 'numvoie': 20, 'libcom': 'LILLE', 'cedex': '', 'depet_limit': ['02', '62', '80'], 'sigle': '', 'apen700': '95.11Z', 'l2_normalisee_nom': '', 'indrep': '', 'prenom': 'TONY', 'nomen_long_nom': '', 'id': '1a4e1ecca0488a04a8bccf9ffd4ec3fe7456b236db1eda54147c6551', 'codpos_bur_distrib': '260', 'l2_normalisee_initial': '', 'codpos_depet': '59', 'libapen': 'Reparation d ordinateurs et d equipements peripheriques', 'nom': 'MARTIN'}

{'typvoie': 'AV', 'l3_normalisee': '', 'enseigne': '', 'libvoie': 'DES MIMOSAS', 'nomen_long_text': 'AIR-C', 'apet700': '70.22Z', 'codpos_cp': '45130', 'siren': '823306030', 'l2_normalisee_text': '', 'nomen_long_initial': 'AC', 'nic': '00011', 'numvoie': 12, 'libcom': 'SAINT AY', 'cedex': '', 'depet_limit': ['18', '28', '41', '58', '77', '89', '91'], 'sigle': '', 'apen700': '70.22Z', 'l2_normalisee_nom': '', 'indrep': '', 'prenom': '', 'nomen_long_nom': 'AIR-C', 'id': 'b420b749064951b18ac95cba63d17a6aa74d9fe76856254279ab7665', 'codpos_bur_distrib': '130', 'l2_normalisee_initial': '', 'codpos_depet': '45', 'libapen': 'Conseil pour les affaires et autres conseils de gestion', 'nom': ''}

{'typvoie': 'RUE', 'l3_normalisee': '', 'enseigne': '', 'libvoie': 'NATIONALE', 'nomen_long_text': 'ECCETERA', 'apet700': '47.59B', 'codpos_cp': '74500', 'siren': '493769970', 'l2_normalisee_text': '', 'nomen_long_initial': 'E', 'nic': '00010', 'numvoie': 21, 'libcom': 'EVIAN LES BAINS', 'cedex': '', 'depet_limit': ['01', '73'], 'sigle': '', 'apen700': '47.59B', 'l2_normalisee_nom': '', 'indrep': '', 'prenom': '', 'nomen_long_nom': 'ECCETERA', 'id': '1769f88b368debbf7f4f4d3e22e4fcffb8792bc111d66f0fea701030', 'codpos_bur_distrib': '500', 'l2_normalisee_initial': '', 'codpos_depet': '74', 'libapen': 'Commerce de detail d autres equipements du foyer', 'nom': ''}

{'typvoie': 'RTE', 'l3_normalisee': '', 'enseigne': '', 'libvoie': 'DU VALLON', 'nomen_long_text': '', 'apet700': '47.91A', 'codpos_cp': '83320', 'siren': '823306113', 'l2_normalisee_text': '', 'nomen_long_initial': '', 'nic': '00015', 'numvoie': 1537, 'libcom': 'CARQUEIRANNE', 'cedex': '', 'depet_limit': ['04', '06', '13', '84'], 'sigle': '', 'apen700': '47.91A', 'l2_normalisee_nom': '', 'indrep': '', 'prenom': 'NATHALIE', 'nomen_long_nom': '', 'id': '11e6d4099333c5783a4e333de53904c4e031d87b75738005fed103c0', 'codpos_bur_distrib': '320', 'l2_normalisee_initial': '', 'codpos_depet': '83', 'libapen': 'Vente a distance sur catalogue general', 'nom': 'GAILLARD'}

{'typvoie': 'RUE', 'l3_normalisee': '', 'enseigne': 'MAIRIE', 'libvoie': "DE L'EGLISE", 'nomen_long_text': 'COMMUNE DE FRAILLICOURT', 'apet700': '84.11Z', 'codpos_cp': '08220', 'siren': '210801627', 'l2_normalisee_text': '', 'nomen_long_initial': 'CDF', 'nic': '00014', 'numvoie': None, 'libcom': 'FRAILLICOURT', 'cedex': '', 'depet_limit': ['02', '51', '55'], 'sigle': '', 'apen700': '84.11Z', 'l2_normalisee_nom': '', 'indrep': '', 'prenom': '', 'nomen_long_nom': 'COMMUNE DE FRAILLICOURT', 'id': '6d2e413d07a1877d543b796da92ba7772088481bf8a2c842c3b82ff1', 'codpos_bur_distrib': '220', 'l2_normalisee_initial': '', 'codpos_depet': '08', 'libapen': 'Administration publique generale', 'nom': ''}

{'typvoie': 'RUE', 'l3_normalisee': '', 'enseigne': '', 'libvoie': 'DE LA CORNICHE', 'nomen_long_text': '', 'apet700': '43.22A', 'codpos_cp': '91120', 'siren': '479454928', 'l2_normalisee_text': '', 'nomen_long_initial': '', 'nic': '00017', 'numvoie': 22, 'libcom': 'PALAISEAU', 'cedex': '', 'depet_limit': ['28', '45', '77', '78', '92', '94'], 'sigle': '', 'apen700': '43.22A', 'l2_normalisee_nom': '', 'indrep': '', 'prenom': 'DAVID', 'nomen_long_nom': '', 'id': '307830402c4302451f2a5e236f30b0ed9c3bd9155b345fc7195ed1b3', 'codpos_bur_distrib': '120', 'l2_normalisee_initial': '', 'codpos_depet': '91', 'libapen': 'Travaux d installation d eau et de gaz en tous locaux', 'nom': 'NOBLECOURT'}

[{'typvoie': 'RUE', 'l3_normalisee': '', 'enseigne': '', 'libvoie': 'DE LA CORNICHE', 'nomen_long_text': '', 'apet700': '43.22A', 'codpos_cp': '91120', 'siren': '479454928', 'l2_normalisee_text': '', 'nomen_long_initial': '', 'nic': '00017', 'numvoie': 22, 'libcom': 'PALAISEAU', 'cedex': '', 'depet_limit': ['28', '45', '77', '78', '92', '94'], 'sigle': '', 'apen700': '43.22A', 'l2_normalisee_nom': '', 'indrep': '', 'prenom': 'DAVID', 'nomen_long_nom': '', 'id': '307830402c4302451f2a5e236f30b0ed9c3bd9155b345fc7195ed1b3', 'codpos_bur_distrib': '120', 'l2_normalisee_initial': '', 'codpos_depet': '91', 'libapen': 'Travaux d installation d eau et de gaz en tous locaux', 'nom': 'NOBLECOURT'}, {'typvoie': 'RUE', 'l3_normalisee': '', 'enseigne': '', 'libvoie': 'DE LA CORNICHE', 'nomen_long_text': '', 'apet700': '43.22A', 'codpos_cp': '91120', 'siren': '479454928', 'l2_normalisee_text': '', 'nomen_long_initial': '', 'nic': '00017', 'numvoie': 22, 'libcom': 'PALAISEAU', 'cedex': '', 'depet_limit': ['28', '45', '77', '78', '92', '94'], 'sigle': '', 'apen700': '43.22A', 'l2_normalisee_nom': '', 'indrep': '', 'prenom': 'DAVID', 'nomen_long_nom': '', 'id': '307830402c4302451f2a5e236f30b0ed9c3bd9155b345fc7195ed1b3', 'codpos_bur_distrib': '120', 'l2_normalisee_initial': '', 'codpos_depet': '91', 'libapen': 'Travaux d installation d eau et de gaz en tous locaux', 'nom': 'NOBLECOURT'}, {'typvoie': 'RUE', 'l3_normalisee': '', 'enseigne': '', 'libvoie': 'DE LA CORNICHE', 'nomen_long_text': '', 'apet700': '43.22A', 'codpos_cp': '91120', 'siren': '479454928', 'l2_normalisee_text': '', 'nomen_long_initial': '', 'nic': '00017', 'numvoie': 22, 'libcom': 'PALAISEAU', 'cedex': '', 'depet_limit': ['28', '45', '77', '78', '92', '94'], 'sigle': '', 'apen700': '43.22A', 'l2_normalisee_nom': '', 'indrep': '', 'prenom': 'DAVID', 'nomen_long_nom': '', 'id': '307830402c4302451f2a5e236f30b0ed9c3bd9155b345fc7195ed1b3', 'codpos_bur_distrib': '120', 'l2_normalisee_initial': '', 'codpos_depet': '91', 'libapen': 'Travaux d installation d eau et de gaz en tous locaux', 'nom': 'NOBLECOURT'}, {'typvoie': 'RUE', 'l3_normalisee': '', 'enseigne': '', 'libvoie': 'DE LA CORNICHE', 'nomen_long_text': '', 'apet700': '43.22A', 'codpos_cp': '91120', 'siren': '479454928', 'l2_normalisee_text': '', 'nomen_long_initial': '', 'nic': '00017', 'numvoie': 22, 'libcom': 'PALAISEAU', 'cedex': '', 'depet_limit': ['28', '45', '77', '78', '92', '94'], 'sigle': '', 'apen700': '43.22A', 'l2_normalisee_nom': '', 'indrep': '', 'prenom': 'DAVID', 'nomen_long_nom': '', 'id': '307830402c4302451f2a5e236f30b0ed9c3bd9155b345fc7195ed1b3', 'codpos_bur_distrib': '120', 'l2_normalisee_initial': '', 'codpos_depet': '91', 'libapen': 'Travaux d installation d eau et de gaz en tous locaux', 'nom': 'NOBLECOURT'}, {'typvoie': 'RUE', 'l3_normalisee': '', 'enseigne': '', 'libvoie': 'DE LA CORNICHE', 'nomen_long_text': '', 'apet700': '43.22A', 'codpos_cp': '91120', 'siren': '479454928', 'l2_normalisee_text': '', 'nomen_long_initial': '', 'nic': '00017', 'numvoie': 22, 'libcom': 'PALAISEAU', 'cedex': '', 'depet_limit': ['28', '45', '77', '78', '92', '94'], 'sigle': '', 'apen700': '43.22A', 'l2_normalisee_nom': '', 'indrep': '', 'prenom': 'DAVID', 'nomen_long_nom': '', 'id': '307830402c4302451f2a5e236f30b0ed9c3bd9155b345fc7195ed1b3', 'codpos_bur_distrib': '120', 'l2_normalisee_initial': '', 'codpos_depet': '91', 'libapen': 'Travaux d installation d eau et de gaz en tous locaux', 'nom': 'NOBLECOURT'}, {'typvoie': 'RUE', 'l3_normalisee': '', 'enseigne': '', 'libvoie': 'DE LA CORNICHE', 'nomen_long_text': '', 'apet700': '43.22A', 'codpos_cp': '91120', 'siren': '479454928', 'l2_normalisee_text': '', 'nomen_long_initial': '', 'nic': '00017', 'numvoie': 22, 'libcom': 'PALAISEAU', 'cedex': '', 'depet_limit': ['28', '45', '77', '78', '92', '94'], 'sigle': '', 'apen700': '43.22A', 'l2_normalisee_nom': '', 'indrep': '', 'prenom': 'DAVID', 'nomen_long_nom': '', 'id': '307830402c4302451f2a5e236f30b0ed9c3bd9155b345fc7195ed1b3', 'codpos_bur_distrib': '120', 'l2_normalisee_initial': '', 'codpos_depet': '91', 'libapen': 'Travaux d installation d eau et de gaz en tous locaux', 'nom': 'NOBLECOURT'}]

问题

如您所见,该tmp字典具有正确的值(每个循环所期望的值是不同的)。但是,当我flatten_data在循环之后打印列表时,列表的每个元素都包含完全相同的字典。

为什么会这样呢?

我如何更改此代码,以使列表不重复(如果是问题的话)最后一个元素代替所有先前的元素?

AKX

flattened_dict={}是你的问题。参数默认值仅被评估一次,因此当您不传递命令时,同一命令将被重用。

您会想要的(为了简洁和格式,很抱歉,在移动设备上)

def ...(..., flattened_dict=None):
    if flattened_dict is None:
         flattened_dict = {}

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

为什么元组大小在其列表元素中添加元素后不会改变

为什么在此列表中我得到的最大值为[3]?

为什么从列表[-1]而不是列表[0]开始计算此列表中的元素?(Python 3)

如何删除此列表中的重复元素?

在此列表中替换for循环

如何在此列表理解中不获取重复项?

在d3中添加元素后,为什么.duration会充当.delay?

如何在列表中的列表中追加元素?

为什么JScrollPane中的此列表未对齐?

为什么此列表中没有填充数据?

为什么此列表在球拍中仅包含无效项目

为什么在DOM中追加元素的速度比使用javascript片段快?

追加元素后,事件单击在jquery中的追加元素上不起作用

在python列表中追加元素时如何检查元素?

为什么值在所有列表元素中重复?

在列表中添加元素后如何空白输入字段?

在列表中追加元素以转换为JSON

为什么此列表有错误?

当我尝试在此列表上使用迭代器时,为什么会引发此异常?

在列表中添加元素

如果在此列表中理解如何使用

为什么此脚本仅适用于此列表中的列表项之一?

在列表上追加元素-javascript

Kotlin - 在地图的列表中追加元素 - 预期列表<对象>,发现列表<Kotlin.Unit>

在列表中的元素之间添加元素

在javaFx中,为什么在向窗口添加元素时为什么需要调用“ getChildren()”?

使用一种列表理解后,是否有可能继续在列表中添加元素?

为什么从此列表的长度中减去一个?

为什么不能合并此列表中的项目,但可以将它们相乘?