比较列表 [英] Comparing lists

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

问题描述

我必须列出A和B,可能或可能不相同。如果它们不是相同的
,我希望输出为三个新列表,X,Y和Z,其中X具有

所有A中的元素,但不是在B中,Y包含B中但不在A中的所有

元素.Z将在A和B中具有

的元素。


这样做的一种方法当然是通过列表迭代并比较每个元素的每个元素,但是有更有效的方法吗?


提前致谢!


-

Har du et kj?leskap,har du en TV

S? har du alt du trenger for? leve


-Jokke& Valentinerne

解决方案

Odd-R。写道:

我必须列出A和B,它们可能相同,也可能不相同。如果它们不相同,我希望输出为三个新列表,X,Y和Z,其中X具有A中的所有元素,但不包含在B中,并且Y包含所有
B中但不在A.中的元素Z将在A和B中具有
元素。


这些是设置操作。 />
这样做的一种方法当然是通过列表迭代并比较每个元素,但是有更有效的方法吗?



也许,使用套装?


L1 = [1,2,3,4]

L2 = [3,4,5,6 ]

diff1 = list(set(L1)-set(L2))#[1,2]

diff2 = list(set(L2)-set(L1) )#[5,6]

symdiff = diff1 + diff2#对称差异[1,2,5,6]

intersect = set(L1 + L2) - set (symdiff)#Interect [3,4]


Best,


Les


尝试使用set。

L1 = [1,1,2,3,4]

L2 = [1,3,99]

A =设定(L1)

B =设定(L2)


X = AB

打印X


Y = BA

打印Y


Z = A | B

打印Z


干杯,

pujo


< AJ **** @ gmail.com>在消息中写道

news:11 ********************** @ g47g2000cwa.googlegr oups.com ...

尝试使用set。
L1 = [1,1,2,3,4]
L2 = [1,3,99]
A = set(L1)< br => B =设定(L2)

X = AB
打印X

Y = BA
打印Y
Z = A | B
打印Z




但是如何高效这是?在这一点上,你能否更明确一点

?什么是订单的复杂性([...])或A-B,B-A,

A | B,A ^ B和A& B' - Python文档

在这方面让我完全处于黑暗中。


对两个列表进行排序然后解压缩

AB ,BA,A | B,A& B和A ^ B在单一

通行证中对我来说很可能更快更好

对于大型列表。


此致,

Christian


I have to lists, A and B, that may, or may not be equal. If they are not
identical, I want the output to be three new lists, X,Y and Z where X has
all the elements that are in A, but not in B, and Y contains all the
elements that are B but not in A. Z will then have the elements that are
in both A and B.

One way of doing this is of course to iterate throug the lists and compare
each of the element, but is there a more efficient way?

Thanks in advance!

--
Har du et kj?leskap, har du en TV
s? har du alt du trenger for ? leve

-Jokke & Valentinerne

解决方案

Odd-R. wrote:

I have to lists, A and B, that may, or may not be equal. If they are not
identical, I want the output to be three new lists, X,Y and Z where X has
all the elements that are in A, but not in B, and Y contains all the
elements that are B but not in A. Z will then have the elements that are
in both A and B.

These are set operations.
One way of doing this is of course to iterate throug the lists and compare
each of the element, but is there a more efficient way?


Maybe, using sets?

L1 = [1,2,3,4]
L2=[3,4,5,6]
diff1 = list(set(L1)-set(L2)) # [1,2]
diff2 = list(set(L2)-set(L1)) # [5,6]
symdiff = diff1+diff2 # Symmetric difference [1,2,5,6]
intersect = set(L1+L2) - set(symdiff) # Intersect [3,4]

Best,

Les


try to use set.
L1 = [1,1,2,3,4]
L2 = [1,3, 99]
A = set(L1)
B = set(L2)

X = A-B
print X

Y = B-A
print Y

Z = A | B
print Z

Cheers,
pujo


<aj****@gmail.com> wrote in message
news:11**********************@g47g2000cwa.googlegr oups.com...

try to use set.
L1 = [1,1,2,3,4]
L2 = [1,3, 99]
A = set(L1)
B = set(L2)

X = A-B
print X

Y = B-A
print Y

Z = A | B
print Z



But how "efficient" is this? Could you be a bit
more explicit on that point? What is the order
of complexity of set([...]) or of A-B, B-A,
A | B, A ^ B and A & B? - The Python Documentation
leaves me completely in the dark in this regard.

Sorting the two lists and then extracting
A-B, B-A, A|B, A & B and A ^ B in one single
pass seems to me very likely to be much faster
for large lists.

Regards,
Christian


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

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