python根据相同的值组合字典



我有一个这样的字典列表

[
{'id': 'A123',
'feature': {'name': 'jack', 'age' : '18' },
'create_time': '2022-5-17 10:29:47',
'is_fast': False},

{'id': 'A123',
'feature': {'gender': 'male'},
'create_time': '2022-5-17 10:29:47',
'is_fast': False},

{'id': 'A123',
'habit': {'name': 'read'},
'create_time': '2022-5-15 10:29:45',
'is_fast': False},
{'id': 'A456',
'feature': {'name': 'rose'},
'create_time': '2022-4-15 10:29:45',
'is_fast': False},

{'id': 'A456',
'habit': {'name': 'sport'},
'create_time': '2022-3-15 10:29:45',
'is_fast': False}
]

但是我想合并相同的"id"值一起使用函数

期望的输出如下

[
{'id': 'A123',
'feature': {'name': 'jack', 'age' : '18' ,'gender': 'male'},
'habit': {'name': 'read'},
'create_time': '2022-5-19 10:29:47',  #Get the latest time based on the same id
'is_fast': False},
{'id': 'A456',
'feature': {'name': 'rose'},
'habit': {'name': 'sport'},
'create_time': '2022-4-15 10:29:45',
'is_fast': False},

]

如何合并相同的"id"根据这些字典的值…

这应该可以让你开始…我放了一些内联注释来解释代码在做什么。您仍然需要实现日期时间比较。

def merge_dicts(lst):
final = {}    #  results
for row in lst:   # iterate through list
if row['id'] not in final:   # if current item id hasnt been seen
final[row['id']] = row   # assign it to results with id as the key
else:
record = final[row['id']]  # otherwise compare to data already stored
for k,v in row.items():   #iterate through dictionary items
if k not in record:   # if key not in results
record[k] = v     # add the key and value
continue
if record[k] == v: continue  # if they are already equal move on
if isinstance(v, dict):    # if its a dictionary
record[k].update(v)    # update the dictionary
else:  # must be date time sequence so do some datetime comparison
"""Do some date comparison and assign correct date"""
return [v for k,v in final.items()]  # convert to list 

print(merge_dicts(lst))

输出:

[
{
'id': 'A123', 
'feature': {'name': 'jack', 'age': '18', 'gender': 'male'}, 
'create_time': '2022-5-17 10:29:47', 
'is_fast': False, 
'habit': {'name': 'read'}
}, 
{
'id': 'A456', 
'feature': {'name': 'rose'}, 
'create_time': '2022-4-15 10:29:45', 
'is_fast': False, 
'habit': {'name': 'sport'}
}
]

您可以使用dict.setdefault方法初始化不存在的键下的子键,以避免使用测试键是否存在的条件语句使代码混乱:

merged = {}
for d in lst:
s = merged.setdefault(d['id'], d)
for k, v in d.items():
if isinstance(v, dict):
s.setdefault(k, v).update(v)
elif v > s[k]: # the dates/times in the input follow alphabetical order
s[k] = v # later dates/times takes precedence
print(list(merged.values()))

演示:https://replit.com/@blhsing BlandCarelessPolygons # main.py

相关内容

  • 没有找到相关文章

最新更新