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


Groups > comp.lang.python > #36204

Re: reduce expression to test sublist

Path csiph.com!newsfeed.hal-mli.net!feeder3.hal-mli.net!newsfeed.hal-mli.net!feeder1.hal-mli.net!nntp-feed.chiark.greenend.org.uk!ewrotcd!news.nosignal.org!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <yacinechaouche@yahoo.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.031
X-Spam-Evidence '*H*': 0.94; '*S*': 0.00; 'skip:[ 20': 0.03; 'false.': 0.07; 'subject:test': 0.07; 'moreover,': 0.09; 'lambda': 0.16; 'message-id:@web125505.mail.ne1.yahoo.com': 0.16; 'subject:expression': 0.16; 'sublist': 0.16; 'element': 0.17; 'integer': 0.17; 'to:name:python-list@python.org': 0.20; 'to:2**1': 0.23; 'header:In-Reply-To:1': 0.25; 'skip:[ 10': 0.26; "doesn't": 0.28; 'correct': 0.28; 'prints': 0.29; 'strings,': 0.29; 'url:mailman': 0.29; 'checks': 0.30; 'lists': 0.31; 'url:python': 0.32; '-----': 0.32; 'print': 0.32; 'url:listinfo': 0.32; 'to:addr:python-list': 0.33; 'reply-to:addr:yahoo.com': 0.34; 'list': 0.35; 'false': 0.35; 'expected': 0.35; 'subject:': 0.36; 'but': 0.36; 'url:org': 0.36; 'email addr:python.org': 0.36; 'test': 0.36; 'sent:': 0.37; 'subject:: ': 0.38; 'from:': 0.38; 'gives': 0.39; 'to:addr:python.org': 0.39; 'help': 0.40; 'url:mail': 0.40; 'email name:python-list': 0.62; 'results': 0.65; 'header:Reply-To:1': 0.68; '2013': 0.84; '7:25': 0.84; 'why?': 0.84; 'received:98.138.226': 0.91
X-Yahoo-Newman-Property ymail-3
X-Yahoo-Newman-Id 109310.55011.bm@omp1030.mail.ne1.yahoo.com
DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=yahoo.com; s=s1024; t=1357415997; bh=MQ97N++BZFTR1a9O+dB2Usv/QhkcqudNtopAWFPUC6Q=; h=X-YMail-OSG:Received:X-Rocket-MIMEInfo:X-Mailer:References:Message-ID:Date:From:Reply-To:Subject:To:In-Reply-To:MIME-Version:Content-Type:Content-Transfer-Encoding; b=ALh+b6cmfyALY41wnbwHWMTKtOP+DDLhvLztpRPeAcYX1KL1ckTD6gatANHA4LhUtGwSAcPz2kZEbVvd4kF/oFAqTa5J6Mtd9ymm52CGc3Np/baaC5OeY4uL3cCz1cP5StlYmFKnHltFutSdXCOhfzfmJdO5saPveUdHisWbsdA=
DomainKey-Signature a=rsa-sha1; q=dns; c=nofws; s=s1024; d=yahoo.com; h=X-YMail-OSG:Received:X-Rocket-MIMEInfo:X-Mailer:References:Message-ID:Date:From:Reply-To:Subject:To:In-Reply-To:MIME-Version:Content-Type:Content-Transfer-Encoding; b=22zOoJW/Q8+FbM1nxN2fBbTcX/grPhN7eMGTXUQABtkP1mxeoS0nRW52DEsTLlq9/NaqZ2iAth2e6qiM3no//wBYesiVZOjvgyzpCGbaHldnmRlFo+01rtq2gNUJmjdvvbCiQuFj+QPGax2YaS7CwFpdIXTKCmJX1g6a9J4mn/Q=;
X-YMail-OSG th8JNzwVM1kGvKQKWOSuP.6bnycMYMJ7wgo4cNsQZsuZMVT G39shXd66q4DqsqeuFkeSbMgDaPhDSOFszMAwxZnvacnebSwqYP0zqscfiM5 eDPzQwf9UNEwzekG_1SIs7yZjgGbHnVzuSbNfKWzxacs5Gfdm8ztTudGno.. KbVKfIOEHsBlU4Jph2llQCeRRXEe79a.ZT3m8VR7pbx_c63gCiAzOCUVzndO YLUNtVNyIYlmCVwdQd8uGynMJ.GNKbGw48LPQW0o1ldzUGoo_6sDjbSkAO8O pibn4.T.tA1rLK.XkU2Q4XYtIz_.ePPrac08mSfvl3Nv6TiZhsxcbCy8AX8t 4.eu.S1R1yX5Ee87ad.tkEEiO1l76RZhV5veoEMw.YCTYhEBiEkLi0Yf8LHL nusk_5uLpulG7m_gbpgBfiUbshtl1xBsEgluK77LxlYZZ7weD_yLmrCVZgXV MmFrkSlATlpcNTIQRViPvhX0vhp56.yeo37FB19IJ0qSus4DgOhmBnQIYGfH kCfyJKhDDWerPGuQfcqkmgpB4pnY5gI4rZvdfvY8-
X-Rocket-MIMEInfo 001.001, QmVjYXVzZSByZWR1Y2UgZG9lc24ndCBkbyB3aGF0IHlvdSB3YW50LiBZb3UnZCB3YW50ICJhbGwiLgoKTDEgPSBbMSwyLDNdCkwyID0gWyJBMSIsIkIyIiwiQzMiLDEsMiwzXQpwcmludCBhbGwoKHggaW4gTDIgZm9yIHggaW4gTDEpKSAjIHByaW50cyBUcnVlCkwzID0gWyJBMSIsIkIyIiwiQzMiXQpwcmludCBhbGwoKHggaW4gTDIgZm9yIHggaW4gTDMpKSAjIHByaW50cyBUcnVlCgoKCgotLS0tLSBPcmlnaW5hbCBNZXNzYWdlIC0tLS0tCkZyb206IEFzaW0gPGFzaW0uci5wQGdtYWlsLmNvbT4KVG86IHB5dGgBMAEBAQE-
X-Mailer YahooMailWebService/0.8.129.483
References <76cd3945-392e-40d4-9f87-d3956b9521d2@googlegroups.com>
Date Sat, 5 Jan 2013 11:59:56 -0800 (PST)
From chaouche yacine <yacinechaouche@yahoo.com>
Subject Re: reduce expression to test sublist
To Asim <asim.r.p@gmail.com>, "python-list@python.org" <python-list@python.org>
In-Reply-To <76cd3945-392e-40d4-9f87-d3956b9521d2@googlegroups.com>
MIME-Version 1.0
Content-Type text/plain; charset=iso-8859-1
Content-Transfer-Encoding quoted-printable
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.15
Precedence list
Reply-To chaouche yacine <yacinechaouche@yahoo.com>
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.140.1357416164.2939.python-list@python.org> (permalink)
Lines 16
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1357416164 news.xs4all.nl 6875 [2001:888:2000:d::a6]:47893
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:36204

Show key headers only | View raw


Because reduce doesn't do what you want. You'd want "all".

L1 = [1,2,3]
L2 = ["A1","B2","C3",1,2,3]
print all((x in L2 for x in L1)) # prints True
L3 = ["A1","B2","C3"]
print all((x in L2 for x in L3)) # prints True




----- Original Message -----
From: Asim <asim.r.p@gmail.com>
To: python-list@python.org
Cc: 
Sent: Saturday, January 5, 2013 7:25 PM
Subject: reduce expression to test sublist

Hi All

The following reduce expression checks if every element of list lst1 is present in list lst2.  It works as expected for integer lists but for lists of strings, it always returns False.

   reduce( lambda x,y: (x in lst2) and (y in lst2), lst1)

Moreover, for the lists of strings the following for-loop gives correct results when the above reduce expression doesn't.

   isSublist = True
   for i in lst1:
      isSublist = isSublist and (i in lst2)
      if not isSublist:
         isSublist = False
         break


Can someone help me understand why?

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

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


Thread

reduce expression to test sublist Asim <asim.r.p@gmail.com> - 2013-01-05 10:25 -0800
  Re: reduce expression to test sublist Dave Angel <d@davea.name> - 2013-01-05 13:58 -0500
  Re: reduce expression to test sublist chaouche yacine <yacinechaouche@yahoo.com> - 2013-01-05 11:59 -0800
  Re: reduce expression to test sublist Jussi Piitulainen <jpiitula@ling.helsinki.fi> - 2013-01-05 22:41 +0200
  Re: reduce expression to test sublist Terry Reedy <tjreedy@udel.edu> - 2013-01-05 16:55 -0500
  Re: reduce expression to test sublist Terry Reedy <tjreedy@udel.edu> - 2013-01-05 16:55 -0500
  Re: reduce expression to test sublist Dave Angel <d@davea.name> - 2013-01-05 17:05 -0500

csiph-web