python 比较两个列表之间的差异
python compare difference between two lists
我有两个这样的列表:
newList = (
(1546, 'John'),
(8794, 'Michael'),
(892416, 'Dave'),
(456789, 'Lucy'),
)
oldList = (
(1546, 'John'),
(8794, 'Michael'),
(892416, 'Dave'),
(246456, 'Alexander')
)
我想要一个比较两个列表的函数。它会是这样的:
def compare(new, old):
print('Alexander is not anymore in the new list !')
print('Lucy is new !')
return newList
我想比较每个人的id是唯一的。
编辑:结果将是我的函数比较。它打印差异。
我不知道如何开始
您可以使用 set
:
def compare(old, new):
oldSet = set(old)
newSet = set(new)
removedElements = oldSet - newSet
newElements = newSet - oldSet
for element in removedElements:
print(element[1] + " is not anymore in the new list!")
for element in newElements:
print(element[1] + " is new!")
这是比较整个元素(id、name)的方法,所以如果你只想比较id,你应该做一些修改(例如使用dicts)。
您可以将列表转换为集合并取差
n = set(l[1] for l in newList)
o = set(l[1] for l in oldList)
print n - o # set(['Lucy'])
print o - n # set(['Alexander'])
编辑:我在对集合了解很多之前就写了这篇文章。现在我会推荐使用下面给出的集合的解决方案。
一个解决方案:
removed = [o for o in old if o[0] not in [n[0] for n in new]]
added = [n for n in new if n[0] not in [o[0] for o in old]]
或者,如果您将数据显示为字典:
old = dict(old) # if you do go for this approach be sure to build these
new = dict(new) # variables as dictionaries, not convert them each time
removed = {k:old[k] for k in old.keys() - new.keys()}
added = {k:new[k] for k in new.keys() - old.keys()}
两者都变成函数,返回 ys
中的项目,但不在 xs
中:
def tuple_list_additions(xs,ys):
return [y for y in ys if y[0] not in [x[0] for x in xs]]
def dict_additions(xs,ys):
return {k:ys[k] for k in ys.keys() - xs.keys()}
我有两个这样的列表:
newList = (
(1546, 'John'),
(8794, 'Michael'),
(892416, 'Dave'),
(456789, 'Lucy'),
)
oldList = (
(1546, 'John'),
(8794, 'Michael'),
(892416, 'Dave'),
(246456, 'Alexander')
)
我想要一个比较两个列表的函数。它会是这样的:
def compare(new, old):
print('Alexander is not anymore in the new list !')
print('Lucy is new !')
return newList
我想比较每个人的id是唯一的。
编辑:结果将是我的函数比较。它打印差异。 我不知道如何开始
您可以使用 set
:
def compare(old, new):
oldSet = set(old)
newSet = set(new)
removedElements = oldSet - newSet
newElements = newSet - oldSet
for element in removedElements:
print(element[1] + " is not anymore in the new list!")
for element in newElements:
print(element[1] + " is new!")
这是比较整个元素(id、name)的方法,所以如果你只想比较id,你应该做一些修改(例如使用dicts)。
您可以将列表转换为集合并取差
n = set(l[1] for l in newList)
o = set(l[1] for l in oldList)
print n - o # set(['Lucy'])
print o - n # set(['Alexander'])
编辑:我在对集合了解很多之前就写了这篇文章。现在我会推荐使用下面给出的集合的解决方案。
一个解决方案:
removed = [o for o in old if o[0] not in [n[0] for n in new]]
added = [n for n in new if n[0] not in [o[0] for o in old]]
或者,如果您将数据显示为字典:
old = dict(old) # if you do go for this approach be sure to build these
new = dict(new) # variables as dictionaries, not convert them each time
removed = {k:old[k] for k in old.keys() - new.keys()}
added = {k:new[k] for k in new.keys() - old.keys()}
两者都变成函数,返回 ys
中的项目,但不在 xs
中:
def tuple_list_additions(xs,ys):
return [y for y in ys if y[0] not in [x[0] for x in xs]]
def dict_additions(xs,ys):
return {k:ys[k] for k in ys.keys() - xs.keys()}