匹配一套字典。最优雅的解决方案。蟒蛇 [英] Match set of dictionaries. Most elegant solution. Python
问题描述
我需要找到差异,并产生新的字典列表,其中只会出现新词典中的对象,并从旧字典中更新属性。
示例:
list_new = [
pre>
{'id':1,
'name':'bob',
'desc':'cool gay '
},
{'id':2,
'name':'Bill',
'desc':'bad gay'
},
{'id':3,
'name':'Vasya',
'desc':无
},
]
list_old = [
{'id':1,
'name':'boby',
'desc':'cool gay',
'some_data':'12345'
},
{'id':2,
'name':'Bill',
'desc' :'cool gay',
'some_data':'12345'
},
{'id':3,
'name':'vasya'
'desc':'the man',
'some_data':'12345'
},
{'id':4,
'name' Elvis',
'desc':'singer',
'some_data':'12345'
},
]
所以..在这个例子中,我想要生成新的列表,只有新的同志从list_new和更新的数据。符合
id
。所以鲍勃会变成鲍比,比尔会成为同性恋,瓦西亚变成了这个男人。结束埃尔维斯不得不缺席。
给我一个优雅的解决方案。有更少的迭代循环。
有办法,我解决了。哪个不是最好的:
def match_dict(new_list,old_list)
ids_new = []
new_list中的item
ids_new.append(item ['id'])
result = []
for old_medias中的item_old:
如果item_old ['id'] in ids_new:
在new_list中的item_new:
如果item_new ['id'] = item_old ['id']
item_new ['some_data'] = item_old ['some_data']
结果。 append(item_new)
返回结果
我有疑问的原因是因为有循环内循环。如果将有2000个项目的列表,则该过程将需要相同的时间。
解决方案步骤:
- 创建通过id查找list_old的查找字典
- 循环通过list_new创建一个合并的dict,如果它存在于旧的
代码:
def match_dict(new_list,old_list):
old = dict(( v ['id'],v)for v in old_list)
return [dict(d,** old [d ['id']])for d in new_list如果d ['id'] in old]
编辑:功能中错误命名的变量。
Given two lists of dictionaries, new one and old one. Dictionaries represent the same objects in both lists.
I need to find differences and produce new list of dictionaries where will be objects from new dictionaries only and updated attributes from old dictionaries.
Example:
list_new=[
{ 'id':1,
'name':'bob',
'desc': 'cool gay'
},
{ 'id':2,
'name':'Bill',
'desc': 'bad gay'
},
{ 'id':3,
'name':'Vasya',
'desc': None
},
]
list_old=[
{ 'id':1,
'name':'boby',
'desc': 'cool gay',
'some_data' : '12345'
},
{ 'id':2,
'name':'Bill',
'desc': 'cool gay',
'some_data' : '12345'
},
{ 'id':3,
'name':'vasya',
'desc': 'the man',
'some_data' : '12345'
},
{ 'id':4,
'name':'Elvis',
'desc': 'singer',
'some_data' : '12345'
},
]
So.. In that example i want produce new list where will be only new gays from list_new with updated data. Matched by id
. So Bob will become Boby, Bill will become coll gay, Vasya become - the man. End Elvis have to be absent.
Give me an elegant solution. With less amount of iteration loops.
There is way, i resolve that. Which is not the best:
def match_dict(new_list, old_list)
ids_new=[]
for item in new_list:
ids_new.append(item['id'])
result=[]
for item_old in old_medias:
if item_old['id'] in ids_new:
for item_new in new_list:
if item_new['id']=item_old['id']
item_new['some_data']=item_old['some_data']
result.append(item_new)
return result
The reason why i'm doubt, because there is loop inside loop. If there will be lists of 2000 items the process would take same time.
Steps:
- Create a look up dictionary for list_old by id
- Loop through list_new dicts creating a merged dict for each if it existed in old
Code:
def match_dict(new_list, old_list):
old = dict((v['id'], v) for v in old_list)
return [dict(d, **old[d['id']]) for d in new_list if d['id'] in old]
EDIT: incorrectly named variables inside function.
这篇关于匹配一套字典。最优雅的解决方案。蟒蛇的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!