测试列表是否包含另一个列表 [英] Test if list contains another list

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

问题描述

你好,


我正在尝试编写一些非常简单的东西来测试一个列表

是否包含另一个:

a = [1,2,3]


b = [3,2,1,4]


但是'' a in b''返回False。我怎样检查a确实包含了

b?


谢谢

解决方案

mathieu写道:


你好,


我想写一些非常简单的东西来测试一下清单

包含另一个:


a = [1,2,3]


b = [3,2 ,1,4]


但是''in b''返回False。我怎样检查b确实包含了




使用套数:


>> a = [1,2,3]
b = [3,2,1,4]
设置(a).issubset(set(b))



True

Christian




>> a = [1,2,3]
b = [3,2,1,4]
a = set(a)
b = set(b)
a.intersection(b)



set([1,2,3])


这就是你想要的吗?


欢呼

詹姆斯


2008年9月8日,mathieu< ma ************* **@gmail.com写道:


你好,


我想写一些非常简单的东西来测试一下list

包含另一个:


a = [1,2,3]


b = [3,2,1,4]

但''在b'中'返回False。如何检查a确实包含

b?


谢谢


-
http://mail.python.org/mailman/listinfo/python -list



-

-

- "问题解决了方法


9月8日上午9:32,Bruno Desthuilliers

< bdesth.quelquech ... @ free.quelquepart.frwrote :


mathieuaécrit:


你好,


我正在尝试编写一些非常简单的东西来测试一个列表

是否包含另一个:


a = [1,2,3]


b = [3,2,1,4]


但''在b''返回False。



的确如此。列表不是集合,并且列表a

的所有元素恰好也是列表b的一部分这一事实并不会使列表本身成为

元素list b。


>> a = [1,2,3]

> > b = [3,2,1,4]

>> c = [b,a]

>> a in c



True


>> b in c



True


>> c



[[3,2,1,4],[1,2,3]]


>>>


如何检查a确实包含

in b?



但这就是你所做的 - 你*确认*是否包含在b中,并且

这不是案件。你想要的是检查一下

的*所有元素*是否包含在b中,这是另一个问题。现在回答

你的问题:使用套装。


>> set(a) .issubset(set(b))



True


>>>



谢谢大家!


Hi there,

I am trying to write something very simple to test if a list
contains another one:

a = [1,2,3]

b = [3,2,1,4]

but ''a in b'' returns False. How do I check that a is indeed contained
in b ?

thanks

解决方案

mathieu wrote:

Hi there,

I am trying to write something very simple to test if a list
contains another one:

a = [1,2,3]

b = [3,2,1,4]

but ''a in b'' returns False. How do I check that a is indeed contained
in b ?

Use sets:

>>a = [1,2,3]
b = [3,2,1,4]
set(a).issubset(set(b))

True
Christian


Hi,

>>a = [1,2,3]
b = [3,2,1,4]
a = set(a)
b = set(b)
a.intersection(b)

set([1, 2, 3])

Is this what you want ?

cheers
James

On 9/8/08, mathieu <ma***************@gmail.comwrote:

Hi there,

I am trying to write something very simple to test if a list
contains another one:

a = [1,2,3]

b = [3,2,1,4]

but ''a in b'' returns False. How do I check that a is indeed contained
in b ?

thanks

--
http://mail.python.org/mailman/listinfo/python-list


--
--
-- "Problems are solved by method"


On Sep 8, 9:32 am, Bruno Desthuilliers
<bdesth.quelquech...@free.quelquepart.frwrote:

mathieu a écrit :

Hi there,

I am trying to write something very simple to test if a list
contains another one:

a = [1,2,3]

b = [3,2,1,4]

but ''a in b'' returns False.


Indeed. Lists are not sets, and the fact that all elements of list a
happens to also be part of list b doesn''t make the list a itself an
element of list b.

>>a = [1, 2, 3]
>>b = [3,2,1,4]
>>c = [b, a]
>>a in c

True

>>b in c

True

>>c

[[3, 2, 1, 4], [1, 2, 3]]

>>>

How do I check that a is indeed contained
in b ?


But that''s what you did - you *did* checked if a was contained in b, and
this is not the case. What you want is to check if *all elements* of a
are contained in b, which is quite another problem. Now the answer to
your question : use sets.

>>set(a).issubset(set(b))

True

>>>

thanks all !


这篇关于测试列表是否包含另一个列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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