Bou*_*TAC 1 python compare list
我有两个这样的列表:
newList = (
(1546, 'John'),
(8794, 'Michael'),
(892416, 'Dave'),
(456789, 'Lucy'),
)
oldList = (
(1546, 'John'),
(8794, 'Michael'),
(892416, 'Dave'),
(246456, 'Alexander')
)
Run Code Online (Sandbox Code Playgroud)
我想要一个比较两个列表的功能.它会是这样的:
def compare(new, old):
print('Alexander is not anymore in the new list !')
print('Lucy is new !')
return newList
Run Code Online (Sandbox Code Playgroud)
我想与每个人的身份比较独特.
编辑:结果将是我的功能比较.它打印出差异.我不知道如何开始
您可以将列表转换为集合并消除差异
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'])
Run Code Online (Sandbox Code Playgroud)