我有两个嵌套字典列表:
lofd1 = [{'A': {'facebook':{'handle':'https://www.facebook.com/pages/New-Jersey/108325505857259','logo_id': None}, 'contact':{'emails':['nj@nj.gov','state@nj.gov']},'state': 'nj', 'population':'12345', 'capital':'Jersey','description':'garden state'}}]
lofd2 = [{'B':{'building_type':'ranch', 'city':'elizabeth', 'state':'nj', 'description':'the state close to NY'}}]
我需要:
最终数据集应如下所示:
lofd_final = [{'state': 'nj', 'facebook':{'handle':'https://www.facebook.com/pages/New-Jersey/108325505857259'},'population':'12345', 'capital':'Jersey', 'contact':{'emails':['nj@nj.gov','state@nj.gov']}, 'description': ['garden state','the state close to NY'],'building_type':'ranch', 'city':'elizabeth'}]
什么是有效的解决方案?
答案 0 :(得分:0)
这是非常适合您的情况的解决方案。就时间复杂度而言; O(n*m)
,n
是列表中词典的数量,m
是字典中键的数量。您只需要一次查看每个词典中的每个键。
def extract_data(lofd, output):
for d in lofd:
for top_level_key in d: # This will be the A or B key from your example
data = d[top_level_key]
state = data['state']
if state not in output: # Create the state entry for the first time
output[state] = {}
# Now update the state entry with the data you care about
for key in data:
# Handle descriptions
if key == 'description':
if 'description' not in output[state]:
output[state]['description'] = [data['description']]
else:
output[state]['description'].append(data['description'])
# Handle all other keys
else:
# Handle facebook key (exclude logo_id)
if key == 'facebook':
del data['facebook']['logo_id']
output[state][key] = data[key]
output = {}
extract_data(lofd1, output)
extract_data(lofd2, output)
print(list(output.values()))
output
将是一个命令,以顶级键为状态。要将其转换为指定的方式,只需将值提取到平面列表中即可:list(output.values())
(请参见上面的示例)。
注意:我假设不需要深层副本。因此,在提取数据之后,我假设您不去操作lofd1
和lofd2
中的值。这也完全基于给出的规格,例如如果需要排除更多嵌套键,则需要自己添加额外的过滤器。