语境
我有一个包含字典的列表。我想“拉平”列表的所有字典(不带嵌套值的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
在循环之后打印列表时,列表的每个元素都包含完全相同的字典。
题
为什么会这样呢?
我如何更改此代码,以使列表不重复(如果是问题的话)最后一个元素代替所有先前的元素?
flattened_dict={}
是你的问题。参数默认值仅被评估一次,因此当您不传递命令时,同一命令将被重用。
您会想要的(为了简洁和格式,很抱歉,在移动设备上)
def ...(..., flattened_dict=None):
if flattened_dict is None:
flattened_dict = {}
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句