Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!weretis.net!feeder1.news.weretis.net!feeder.erje.net!newsfeed.xs4all.nl!newsfeed6.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.002 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'example:': 0.03; 'operator': 0.05; 'received:localnet': 0.07; 'python': 0.08; 'content-type:multipart/signed': 0.09; 'filename:fname piece:signature': 0.09; 'garbage': 0.09; 'loop.': 0.09; 'answer,': 0.16; 'content-type:application/pgp-signature': 0.16; 'filename:fname piece:asc': 0.16; 'filename:fname:signature.asc': 0.16; 'fingerprint:': 0.16; 'slice,': 0.16; 'wrote:': 0.16; 'subject:list': 0.18; 'memory': 0.21; 'header:In-Reply-To:1': 0.22; 'lists': 0.28; 'problem': 0.28; 'far.': 0.29; 'id:': 0.29; 'second': 0.29; 'print': 0.29; 'received:209.85.161.46': 0.31; 'received:mail-fx0-f46.google.com': 0.31; 'list': 0.32; 'break': 0.32; 'there': 0.33; 'to:addr:python-list': 0.33; 'instead': 0.33; 'header:User-Agent:1': 0.34; 'totally': 0.35; 'received:209.85.161': 0.35; 'charset:us-ascii': 0.36; 'example,': 0.37; 'list,': 0.37; 'but': 0.37; 'something': 0.37; 'received:google.com': 0.38; 'received:209.85': 0.38; 'subject:: ': 0.39; 'sets': 0.39; 'to:addr:python.org': 0.39; 'hope': 0.61; 'received:77': 0.63; 'url:ip addr': 0.74; '1390': 0.84; 'url:46': 0.84 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=from:to:subject:date:message-id:user-agent:in-reply-to:references :mime-version:content-type:content-transfer-encoding; bh=XNJbgY5q74pn432Tu4G+m4N0bpTh0vXCme9xvUv7E+M=; b=ci13rxBq8z5bDYZUT6hcCMxsxDkUPJj7S0Sp1XEzHDqco+qAZlkNgdG4VWvaAdOE2C X+4u3IBPVry4B4LIoZ/hw8Rh9VUulbVtQd8EzackqxH9lLW17/FHSTCN/6+h+r8nrgRF yz/t7C4yiQ9qYg9IPv5MVG0LSZjOmLdKfgqeA= From: Ameretat Reith To: python-list@python.org Subject: Re: testing if a list contains a sublist Date: Wed, 17 Aug 2011 18:41:21 +0430 User-Agent: KMail/4.7.0 (Linux/2.6.39-gentoo-r2; KDE/4.7.0; x86_64; ; ) In-Reply-To: <4E49AB3E.9000801@web.de> References: <4E49AB3E.9000801@web.de> MIME-Version: 1.0 Content-Type: multipart/signed; boundary="nextPart3265298.UO82fQ5H1C"; micalg="pgp-sha256"; protocol="application/pgp-signature" Content-Transfer-Encoding: 7Bit X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.12 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 52 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1313590402 news.xs4all.nl 23942 [2001:888:2000:d::a6]:50081 X-Complaints-To: abuse@xs4all.nl Xref: x330-a1.tempe.blueboxinc.net comp.lang.python:11685 --nextPart3265298.UO82fQ5H1C Content-Transfer-Encoding: 7Bit Content-Type: text/plain; charset="us-ascii" On Se shanbe 25 Mordad 1390 01:26:54 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. > > greatz Johannes Hope best answer is found so far. for easy answer, i prefer to use Python `==' operator instead of inner loop. l=[1,2,3,4,5] s=[1,2,2] sc=len(s) for c in xrange(len(l)-sc+1): if l[c:sc+c] == s: print ( 'found at {0}'.format(c) ) break Since sub-lists memory will be garbage collected, there is no problem in memory usage, but in time needed for constructing new slice, there is. -- Amir Ghassemi Nasr (Reith) GPG ID: 371035B4 (http://46.4.214.112/~reith/reith.asc) GPG Fingerprint: 18E6 CF11 BE80 F541 DC68 B6AF 9373 DE72 3710 35B4 --nextPart3265298.UO82fQ5H1C Content-Type: application/pgp-signature; name="signature.asc" Content-Description: This is a digitally signed message part. -----BEGIN PGP SIGNATURE----- Version: GnuPG v2.0.18 (GNU/Linux) iF4EABEIAAYFAk5LzA4ACgkQk3PecjcQNbT6WQD/RFeWBcsCRhLo9ESfXRoWxkMH Hw0IGr+gBAW52tWDOKUBAJM+58Nuz4kh6Ouv41mxXwVbWIw5WcnuWPhEE/IGiYh+ =7Qy2 -----END PGP SIGNATURE----- --nextPart3265298.UO82fQ5H1C--