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


Groups > comp.lang.python > #11921

Re: testing if a list contains a sublist

Path csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!aioe.org!feeder.news-service.com!tudelft.nl!txtfeed1.tudelft.nl!multikabel.net!newsfeed10.multikabel.net!xlned.com!feeder7.xlned.com!news2.euro.net!newsgate.cistron.nl!newsgate.news.xs4all.nl!194.109.133.84.MISMATCH!newsfeed.xs4all.nl!newsfeed5.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail
Return-Path <sajmikins@gmail.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.022
X-Spam-Evidence '*H*': 0.96; '*S*': 0.00; 'example:': 0.03; 'def': 0.15; 'received:209.85.213.174': 0.16; 'received:mail- yx0-f174.google.com': 0.16; 'mon,': 0.16; 'wrote:': 0.16; 'cheers,': 0.18; 'subject:list': 0.18; 'header:In-Reply-To:1': 0.22; 'pm,': 0.24; 'aug': 0.24; 'lists': 0.28; 'problem': 0.28; 'url:mailman': 0.28; 'import': 0.28; 'message-id:@mail.gmail.com': 0.29; 'second': 0.29; 'list': 0.32; 'probably': 0.33; 'to:addr :python-list': 0.33; 'url:listinfo': 0.33; 'totally': 0.35; 'url:python': 0.36; 'example,': 0.37; 'list,': 0.37; 'but': 0.37; 'something': 0.37; 'received:google.com': 0.38; 'url:org': 0.38; 'received:209.85': 0.38; 'subject:: ': 0.39; 'sets': 0.39; 'to:addr:python.org': 0.39; '4:26': 0.84
DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :content-type; bh=wACwgQzFQ+cYeHV64jLztP6N/eg55xmMShVPm6G0WD8=; b=FZfpzPQQBA1yMaVT3uG7by8fyH3IVYw1my0pfjrCvrQX7RX1Kw5mlso2iPDJvkXGfn HZCIRwJKkxW7w2odiR8YiCV5WP4fBplqepzdUUGyuf3x8TwnApsvcKJW4ciPJs1z6WRt GqlWPuPdBnzv5Vopq1ttx0vQN+kYKKbBRd66I=
MIME-Version 1.0
In-Reply-To <4E49AB3E.9000801@web.de>
References <4E49AB3E.9000801@web.de>
Date Sat, 20 Aug 2011 09:06:38 -0700
Subject Re: testing if a list contains a sublist
From Simon Forman <sajmikins@gmail.com>
To python-list@python.org
Content-Type text/plain; charset=ISO-8859-1
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.12
Precedence list
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.267.1313856408.27778.python-list@python.org> (permalink)
Lines 34
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1313856408 news.xs4all.nl 23887 [2001:888:2000:d::a6]:36473
X-Complaints-To abuse@xs4all.nl
Xref x330-a1.tempe.blueboxinc.net comp.lang.python:11921

Show key headers only | View raw


On Mon, Aug 15, 2011 at 4:26 PM, Johannes <dajo.mail@web.de> 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.
>
> greatz Johannes
> --
> http://mail.python.org/mailman/listinfo/python-list
>

Probably not the most efficient way, but I wanted to mention it:


from difflib import SequenceMatcher


def list_in(a, b):
    '''Is a completely contained in b?'''
    matcher = SequenceMatcher(a=a, b=b)
    m = matcher.find_longest_match(0, len(a), 0, len(b))
    return m.size == len(a)


Cheers,
~Simon

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


Thread

Re: testing if a list contains a sublist Simon Forman <sajmikins@gmail.com> - 2011-08-20 09:06 -0700

csiph-web