【问题标题】:In this list, why are elements duplicated after appending?在这个列表中,为什么元素在追加后重复?
【发布时间】:2021-01-06 12:45:25
【问题描述】:

上下文

我有一个包含字典的列表。我想“展平”(没有嵌套值的 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 列表时,列表的每个元素都包含完全相同的字典。

问题

为什么会这样?

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

【问题讨论】:

  • 请也显示Utils.flatten_json
  • @AKX flatten_json 已添加到问题中。

标签: python python-3.x list loops


【解决方案1】:

flattened_dict={} 是你的问题。参数默认值只计算一次,所以当你不传入一个 dict 时,同一个会被重用。

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

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

【讨论】:

  • 确实如此。我现在有预期的行为。你知道为什么这是默认行为吗?除非我弄错了,否则我在使用的任何语言(java、c#、js 等)中都没有遇到这种行为。那么为什么这是默认行为呢?这太反直觉了!
  • 我正要发布相同的答案 ;) ...是的,这可能与直觉相反,但默认参数是在 python 中静态创建的。所以最佳实践不是使用可变的默认参数,而是使用所描述的解决方法。这让我想到这种(独特的?)行为是否可以在其他情况下用于好事!?
  • 作为可能需要它的人的来源:docs.python-guide.org/writing/gotchas/…
  • 有时这种行为很有用,例如。 G。缓存。
猜你喜欢
  • 2014-08-15
  • 1970-01-01
  • 1970-01-01
  • 2015-09-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-11-20
相关资源
最近更新 更多