找到两个列表的不同元素

时间:2018-03-24 07:15:35

标签: python

我有两个列表,l1和l2,我想比较列表l1和l2。然后我想  查找彼此不同的列表元素。列表如下:

l1= [('A', '2', '3', '4', '5', '6'), ('F', '2', '4', '2.2', '2.7', '3'), ('G', '5', '2.1', '3.4', '1.5', '2'),('H', 'L', 'L', 'D', 'C', 'B')]

l2= [('A', '2', '3', '4', '5', '6'),('H', 'L', 'L', 'D', 'C', 'B')]

我想要的输出是:

l3=[('F', '2', '4', '2.2', '2.7', '3'), ('G', '5', '2.1', '3.4', '1.5', '2')]

我该怎么做?

2 个答案:

答案 0 :(得分:1)

假设顺序无关紧要,您可以使用uniqueUsers

答案 1 :(得分:0)

这是满足您需求的代码:

output_list = []
if len(l1) < len(l2):
    shortest_list = l1
    other_list = l2
else:
    shortest_list = l2
    other_list = l1
for tup in other_list:
    if tup not in shortest_list:
        output_list.append(tup)
print output_list

输出列表是l3。

我希望它可以帮助你,

DOR

相关问题