给出两个词典列表,新词典和旧词典。字典表示两个列表中的相同对象。
我需要找到差异并生成新的词典列表,其中只有新词典中的对象和旧词典的更新属性。
例如:
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'
},
]
所以..在那个例子中,我希望产生一个新的列表,其中只有来自list_new的新同性恋者和更新的数据。由id
匹配。所以Bob将成为Boby,比尔将成为同性恋,Vasya成为 - 男人。结束猫王必须缺席。
给我一个优雅的解决方案。用较少量的迭代循环。
有办法,我解决了这个问题。哪个不是最好的:
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
我之所以怀疑,因为循环内部存在循环。如果将有2000个项目的列表,则该过程将花费相同的时间。
答案 0 :(得分:3)
不能完全把它放到一行,但这是一个更简单的版本:
def match_new(new_list, old_list) :
ids = dict((item['id'], item) for item in new_list)
return [ids[item['id']] for item in old_list if item['id'] in ids]
答案 1 :(得分:2)
不知道你的数据的约束,我会假设id
在每个列表中是唯一的,并且你的列表只包含可清除的可模拟类型(string,int,...)。
# first index each list by id
new = {item['id']: item for item in list_new}
old = {item['id']: item for item in list_old}
# now you can see which ids appeared in the new list
created = set(new.keys())-set(old.keys())
# or which ids were deleted
deleted = set(old.keys())-set(new.keys())
# or which ids exists in the 2 lists
intersect = set(new.keys()).intersection(set(old.keys()))
# using the same 'conversion to set' trick,
# you can see what is different for each item
diff = {id: dict(set(new[id].items())-set(old[id].items())) for id in intersect}
# using your example data set, diff now contains the differences for items which exists in the two lists:
# {1: {'name': 'bob'}, 2: {'desc': 'bad gay'}, 3: {'name': 'Vasya', 'desc': None}}
# you can now add the new ids to this diff
diff.update({id: new[id] for id in created})
# and get your data back into the original format:
list_diff = [dict(data, **{'id': id}) for id,data in diff.items()]
这是使用python 3语法,但应该很容易移植到python 2。
编辑:这里是为python 2.5编写的相同代码:
new = dict((item['id'],item) for item in list_new)
old = dict((item['id'],item) for item in list_old)
created = set(new.keys())-set(old.keys())
deleted = set(old.keys())-set(new.keys())
intersect = set(new.keys()).intersection(set(old.keys()))
diff = dict((id,dict(set(new[id].items())-set(old[id].items()))) for id in intersect)
diff.update(dict(id,new[id]) for id in created))
list_diff = [dict(data, **{'id': id}) for id,data in diff.items()]
(注意如果没有字典理解,代码的可读性会降低)
答案 2 :(得分:1)
对于old_list中的每个字典,在new_list中搜索具有相同id的字典,然后执行:old_dict.update(new_dict)
在更新之后,从new_list中删除每个new_dict,并在循环之后附加剩余的未使用的dicts。
答案 3 :(得分:1)
这就是你需要的东西:
l = []
for d in list_old:
for e in list_new:
if e['id'] == d['id']:
l.append(dict(e, **d))
print l
阅读有关如何合并词典的here。
答案 4 :(得分:1)
你可以这样做:
def match_dict(new_list, old_list):
new_dict = dict((obj['id'], obj) for obj in new_list)
old_dict = dict((obj['id'], obj) for obj in old_list)
for k in new_dict.iterkeys():
if k in old_dict:
new_dict[k].update(old_dict[k])
else:
del new_dict[k]
return new_dict.values()
如果您经常这样做,我建议将您的数据存储为以字典为键而不是列表的字典,这样您就不必每次都进行转换。
编辑:以下示例说明如何将数据存储在字典中。
list_new = [{'desc': 'cool guy', 'id': 1, 'name': 'bob'}, {'desc': 'bad guy', 'id': 2, 'name': 'Bill'}, {'desc': None, 'id': 3, 'name': 'Vasya'}]
# create a dictionary with the value of 'id' as the key
dict_new = dict((obj['id'], obj) for obj in list_new)
# now you can access entries by their id instead of having to loop through the list
print dict_new[2]
# {'id': 2, 'name': 'Bill', 'desc': 'bad guy'}
答案 5 :(得分:1)
步骤:
代码:
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]
编辑:函数内部命名错误。
答案 6 :(得分:0)
如果您的顶级数据结构是字典而不是列表,那么您会好得多。那就是:
dict_new.update(dict_old)
然而,对于你实际拥有的,试试这个:
result_list = []
for item in list_new:
found_item = [d for d in list_old if d["id"] == item["id"]]
if found_item:
result_list.append(dict(item, **found_item[0]))
这实际上在循环中仍然有一个循环(内循环是"隐藏"在列表理解中)所以它仍然是O(n ** 2)。在大型数据集上,将它转换为dict,更新它,然后将其转换回列表无疑会更快。
答案 7 :(得分:0)
你可以喜欢这个。拜托,谢谢。
def match_dict(new_list, old_list):
id_new = [item_new.get("id") for item_new in list_new]
id_old = [item_old.get("id") for item_old in list_old]
for idx_old in id_old:
if idx_old in id_new:
list_new[id_new.index(idx_old)].update(list_old[id_old.index(idx_old)])
return list_new
from pprint import pprint
pprint(match_dict(list_new, list_old))
输出:
[{'desc': 'cool gay', 'id': 1, 'name': 'boby', 'some_data': '12345'},
{'desc': 'cool gay', 'id': 2, 'name': 'Bill', 'some_data': '12345'},
{'desc': 'the man', 'id': 3, 'name': 'vasya', 'some_data': '12345'}]
答案 8 :(得分:0)
[od for od in list_old if od['id'] in {nd['id'] for nd in list_new}]