Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #36213
| Path | csiph.com!newsfeed.hal-mli.net!feeder3.hal-mli.net!newsfeed.hal-mli.net!feeder1.hal-mli.net!newsfeed.xs4all.nl!newsfeed1.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail |
|---|---|
| Return-Path | <python-python-list@m.gmane.org> |
| X-Original-To | python-list@python.org |
| Delivered-To | python-list@mail.python.org |
| X-Spam-Status | OK 0.000 |
| X-Spam-Evidence | '*H*': 1.00; '*S*': 0.00; 'else:': 0.03; 'true,': 0.04; 'false,': 0.07; 'false.': 0.07; 'stops': 0.07; 'subject:test': 0.07; 'true)': 0.07; 'check.': 0.09; 'does,': 0.09; 'moreover,': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'similar,': 0.09; 'terry': 0.09; 'def': 0.10; 'b):': 0.16; 'comments:': 0.16; 'false:': 0.16; 'hashable': 0.16; 'iterable,': 0.16; 'lambda': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'redundant.': 0.16; 'reedy': 0.16; 'subject:expression': 0.16; 'wrote:': 0.17; 'element': 0.17; 'integer': 0.17; 'jan': 0.18; 'input': 0.18; 'work,': 0.22; 'testing': 0.24; 'header:In-Reply-To:1': 0.25; 'header:User-Agent:1': 0.26; 'setting': 0.26; 'correct': 0.28; 'header:X-Complaints-To:1': 0.28; 'remains': 0.29; 'strings,': 0.29; 'testing.': 0.29; 'checks': 0.30; 'lists': 0.31; 'code': 0.31; 'to:addr:python-list': 0.33; 'done': 0.34; 'list': 0.35; 'needed': 0.35; 'false': 0.35; 'said,': 0.35; 'expected': 0.35; 'pm,': 0.35; 'there': 0.35; 'received:org': 0.36; 'except': 0.36; 'but': 0.36; 'should': 0.36; 'item': 0.37; 'rather': 0.37; 'data': 0.37; 'subject:: ': 0.38; 'gives': 0.39; 'to:addr:python.org': 0.39; 'takes': 0.39; 'header:Received:5': 0.40; 'first': 0.61; 'results': 0.65; 'taking': 0.65; 'account': 0.67; 'received:fios.verizon.net': 0.84; 'items,': 0.91 |
| X-Injected-Via-Gmane | http://gmane.org/ |
| To | python-list@python.org |
| From | Terry Reedy <tjreedy@udel.edu> |
| Subject | Re: reduce expression to test sublist |
| Date | Sat, 05 Jan 2013 16:55:59 -0500 |
| References | <76cd3945-392e-40d4-9f87-d3956b9521d2@googlegroups.com> |
| Mime-Version | 1.0 |
| Content-Type | text/plain; charset=UTF-8; format=flowed |
| Content-Transfer-Encoding | 7bit |
| X-Gmane-NNTP-Posting-Host | pool-173-75-251-66.phlapa.fios.verizon.net |
| User-Agent | Mozilla/5.0 (Windows NT 6.1; WOW64; rv:17.0) Gecko/17.0 Thunderbird/17.0 |
| In-Reply-To | <76cd3945-392e-40d4-9f87-d3956b9521d2@googlegroups.com> |
| X-BeenThere | python-list@python.org |
| X-Mailman-Version | 2.1.15 |
| 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.147.1357423191.2939.python-list@python.org> (permalink) |
| Lines | 63 |
| NNTP-Posting-Host | 2001:888:2000:d::a6 |
| X-Trace | 1357423191 news.xs4all.nl 6854 [2001:888:2000:d::a6]:52135 |
| X-Complaints-To | abuse@xs4all.nl |
| Xref | csiph.com comp.lang.python:36213 |
Show key headers only | View raw
On 1/5/2013 1: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)
reduce(lambda x, y: x and (y in lst2), lst1, True)
would work, but as other have said, you want to stops at the first
False, which all() does, and if lst2 is a list of hashable items, x in
set for O(1) rather than O(n) check.
> Moreover, for the lists of strings the following for-loop gives
> correct results when the above reduce expression doesn't.
You should include data for testing.
>
> isSublist = True
> for i in lst1:
> isSublist = isSublist and (i in> lst2)
If isSublist remains True, there is no need to rebind it
> if not isSublist:
> isSublist = False
Setting isSublist False when it is False is redundant.
> break
Taking into account the comments:
def is_sublist(a, b):
b = set(b) # optional, depending on contents
for item in a:
if item not in b:
return False
else: # not needed because return above
return True
The code for all() is similar, except that all takes an iterable, so
that the testing is done as part of the input iterable.
def all(iterable):
it = iter(iterable):
for item in it:
if not it:
return False:
else:
return True
def issublist(a, b):
b = set(b)
return all(item in b for item in a)
--
Terry Jan Reedy
Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll 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