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


Groups > comp.lang.python > #36204

Re: reduce expression to test sublist

References <76cd3945-392e-40d4-9f87-d3956b9521d2@googlegroups.com>
Date 2013-01-05 11:59 -0800
From chaouche yacine <yacinechaouche@yahoo.com>
Subject Re: reduce expression to test sublist
Newsgroups comp.lang.python
Message-ID <mailman.140.1357416164.2939.python-list@python.org> (permalink)

Show all headers | 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