Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #11579

Re: testing if a list contains a sublist

Path csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!news.dougwise.org!nntpfeed.proxad.net!proxad.net!feeder1-2.proxad.net!usenet-fr.net!de-l.enfer-du-nord.net!feeder1.enfer-du-nord.net!feeder.news-service.com!news2.euro.net!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <python@mrabarnett.plus.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.004
X-Spam-Evidence '*H*': 0.99; '*S*': 0.00; 'example:': 0.03; 'from:addr:python': 0.09; 'solution,': 0.09; '(c1': 0.16; 'class:': 0.16; 'from:addr:mrabarnett.plus.com': 0.16; 'from:name:mrab': 0.16; 'message-id:@mrabarnett.plus.com': 0.16; 'received:84.92': 0.16; 'received:84.92.122': 0.16; 'received:84.92.122.60': 0.16; 'reply-to:addr:python-list': 0.16; 'wrote:': 0.16; 'subject:list': 0.18; '>>>': 0.18; 'header:In- Reply-To:1': 0.22; 'received:84': 0.28; 'lists': 0.28; 'problem': 0.28; 'import': 0.28; 'second': 0.29; 'collections': 0.30; 'list': 0.32; 'to:addr:python-list': 0.33; 'header:User-Agent:1': 0.34; 'reply-to:addr:python.org': 0.34; 'totally': 0.35; 'example,': 0.37; 'using': 0.37; 'list,': 0.37; 'but': 0.37; 'something': 0.37; 'subject:: ': 0.39; 'sets': 0.39; 'to:addr:python.org': 0.39; 'header:Reply-To:1': 0.71; 'reply-to:no real name:2**0': 0.71
X-IronPort-Anti-Spam-Filtered true
X-IronPort-Anti-Spam-Result Ao4HAB+dSk7Unw4S/2dsb2JhbABBmS6PEXeBQAEBBAEnEUARCwgQCRYPCQMCAQIBDTgTCAEBF4dVArlLhkgEi15Ji3eLag
Date Tue, 16 Aug 2011 17:41:55 +0100
From MRAB <python@mrabarnett.plus.com>
User-Agent Mozilla/5.0 (Windows NT 5.1; rv:5.0) Gecko/20110624 Thunderbird/5.0
MIME-Version 1.0
To python-list@python.org
Subject Re: testing if a list contains a sublist
References <4E49AB3E.9000801@web.de>
In-Reply-To <4E49AB3E.9000801@web.de>
Content-Type text/plain; charset=ISO-8859-1; format=flowed
Content-Transfer-Encoding 7bit
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.12
Precedence list
Reply-To python-list@python.org
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <http://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.83.1313512923.27778.python-list@python.org> (permalink)
Lines 32
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1313512923 news.xs4all.nl 23983 [2001:888:2000:d::a6]:57797
X-Complaints-To abuse@xs4all.nl
Xref x330-a1.tempe.blueboxinc.net comp.lang.python:11579

Show key headers only | View raw


On 16/08/2011 00:26, Johannes wrote:
> hi list,
> what is the best way to check if a given list (lets call it l1) is
> totally contained in a second list (l2)?
>
> for example:
> l1 = [1,2], l2 = [1,2,3,4,5] ->  l1 is contained in l2
> l1 = [1,2,2,], l2 = [1,2,3,4,5] ->  l1 is not contained in l2
> l1 = [1,2,3], l2 = [1,3,5,7] ->  l1 is not contained in l2
>
> my problem is the second example, which makes it impossible to work with
> sets insteads of lists. But something like set.issubset for lists would
> be nice.
>
Here's my solution, using the Counter class:

 >>> from collections import Counter
 >>>
 >>> c1 = Counter([1,2])
 >>> c2 = Counter([1,2,3,4,5])
 >>> (c1 & c2) == c1
True
 >>>
 >>> c1 = Counter([1,2,2,])
 >>> c2 = Counter([1,2,3,4,5])
 >>> (c1 & c2) == c1
False
 >>>
 >>> c1 = Counter([1,2,3])
 >>> c2 = Counter([1,3,5,7])
 >>> (c1 & c2) == c1
False

Back to comp.lang.python | Previous | Next | Find similar | Unroll thread


Thread

Re: testing if a list contains a sublist MRAB <python@mrabarnett.plus.com> - 2011-08-16 17:41 +0100

csiph-web