Python中两个列表的有效比较 [英] Efficient Compare of two LIST of Lists in Python

查看:132
本文介绍了Python中两个列表的有效比较的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是python的新手,我在做项目时就在学习东西,这里有两个列表,我需要比较和分离A中找到的差异-> B和diff发现b-> A 最好的比较方法是什么.

I am a newbie to python and just learning things as i do my project and here i have two list of list which i need to compare and separate the diff found in A -- > B and diff found b --> A What is the best way of comparing.

A=[[1L, 'test_case_1'], [1L, 'test_case_2'], [2L, 'test_case_1']]
B=[[1L, 'test_case_1'], [1L, 'test_case_4'], [2L, 'test_case_1'], [2L, 'test_case_3']]

推荐答案

假设您可以按照我的评论使用元组列表,对Junuxx答案的这种简单修改会更加有效

Assuming you can use a list of tuples as per my comment, this simple modification of Junuxx's answer is much more efficient

A-B:

>>> setb = set(B)
>>> [x for x in A if not x in setb]
[(1L, 'test_case_2')]

B-A:

>>> seta = set(A)
>>> [x for x in B if not x in seta]
[(1L, 'test_case_4'), (2L, 'test_case_3')]

这篇关于Python中两个列表的有效比较的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆