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


Groups > comp.lang.python > #36199

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!newsreader4.netcologne.de!news.netcologne.de!newsfeed.freenet.ag!news2.euro.net!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <d@davea.name>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.030
X-Spam-Evidence '*H*': 0.94; '*S*': 0.00; 'false.': 0.07; 'subject:test': 0.07; 'moreover,': 0.09; 'subtract': 0.09; 'suggest': 0.11; 'boolean': 0.16; 'false),': 0.16; 'iterating': 0.16; 'lambda': 0.16; 'subject:expression': 0.16; 'wrote:': 0.17; 'element': 0.17; 'integer': 0.17; 'trying': 0.21; 'elements': 0.23; 'header:In-Reply-To:1': 0.25; 'header:User-Agent:1': 0.26; 'correct': 0.28; 'loop,': 0.29; 'strings,': 0.29; 'maybe': 0.29; 'checks': 0.30; 'compatible': 0.30; 'sense': 0.31; 'lists': 0.31; 'to:addr:python-list': 0.33; 'list': 0.35; 'false': 0.35; 'faster': 0.35; 'expected': 0.35; 'pm,': 0.35; 'something': 0.35; 'list.': 0.35; 'but': 0.36; 'turn': 0.36; 'subject:: ': 0.38; 'some': 0.38; 'gives': 0.39; 'to:addr:python.org': 0.39; 'received:192': 0.39; 'received:192.168': 0.40; 'help': 0.40; 'your': 0.60; "you've": 0.61; 'results': 0.65; 'header:Reply- To:1': 0.68; 'received:74.208': 0.71; 'reply-to:no real name:2**0': 0.72; 'sets,': 0.84; 'why?': 0.84; 'reducing': 0.95
Date Sat, 05 Jan 2013 13:58:10 -0500
From Dave Angel <d@davea.name>
User-Agent Mozilla/5.0 (X11; Linux x86_64; rv:16.0) Gecko/20121011 Thunderbird/16.0.1
MIME-Version 1.0
To python-list@python.org
Subject Re: reduce expression to test sublist
References <76cd3945-392e-40d4-9f87-d3956b9521d2@googlegroups.com>
In-Reply-To <76cd3945-392e-40d4-9f87-d3956b9521d2@googlegroups.com>
Content-Type text/plain; charset=ISO-8859-1
Content-Transfer-Encoding 7bit
X-Provags-ID V02:K0:9sb4ntFFxuBsJKSf674bDzLp1piUHaM6V4WqePyxK+M uuBB8b1xi4Xzntnt/rf+bmWFmd20i14fCkkRcukPW6H0VeFYQ9 PL2+5ZZBDzDBZHnFaDQidfeQ8pGvFSH01htuEVpbStY81MGPBV zPp/FdoSgim5xEG9zuM+3QroMNFPtu9hLLPiHrLdFBTyKn7eru UMBhkj64hRMEK9IemDr81ekgqaPio4Shm7wawO/jEStmsQg8Ny jq9s4h/fpYvFy/OuhlfwcYjp5mTes4G9SVQYAikCH/8/cGicZm CKkNzzA97Df1b2glLNCbkOBv0gmZGs6lVSDM1mZh6RLyxOPCg= =
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.15
Precedence list
Reply-To d@davea.name
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.136.1357412309.2939.python-list@python.org> (permalink)
Lines 39
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1357412309 news.xs4all.nl 6911 [2001:888:2000:d::a6]:33125
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:36199

Show key headers only | View raw


On 01/05/2013 01:25 PM, Asim wrote:
> 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
reduce only makes sense if the value you're reducing to is of the same
type as the elements of the list you're iterating over.  Since your
lambda expression returns a boolean (True or False), it'll seem to work
on some lists of ints.  That's just a coincidence since the bools are
compatible with ints, and maybe you've got a 0 or 1 in the list.  But if
your list has only strings, then True will never be that list.

If you're trying to make a faster loop, then I suggest you look into set
differences.  Turn both lists into sets, and subtract them.   Something
like (untested):

   result =  not bool( set(lst1) - set(lst2) )



-- 

DaveA

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