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


Groups > comp.lang.python > #28954

Re: Boolean function on variable-length lists

From Jussi Piitulainen <jpiitula@ling.helsinki.fi>
Newsgroups comp.lang.python
Subject Re: Boolean function on variable-length lists
Date 2012-09-12 16:02 +0300
Organization University of Helsinki
Message-ID <qotwqzzjswb.fsf@ruuvi.it.helsinki.fi> (permalink)
References <f9d9dfa2-71c6-4b47-9d0a-e6f59c8ab818@googlegroups.com>

Show all headers | View raw


Libra writes:

> Hello,
> 
> I need to implement a function that returns 1 only if all the values
> in a list satisfy given constraints (at least one constraint for
> each element in the list), and zero otherwise.
> 
> For example, I may have a list L = [1, 2, 3, 4] and the following
> constraints:
> L[0] >= 1
> L[1] <= 3
> L[2] == 2
> L[3] >= 3
> 
> In this case, the function returns 0 because the third constraint is
> not satisfied.

So you would associate each constraint with an index. You could
maintain a list of constraints and apply it to the values as follows:

>>> cs = [ lambda x : x >= 1, lambda x : x <= 3, lambda x : x == 2,
...        lambda x : x >= 3 ]
>>> { f(x) for f, x in zip(cs, [1,2,3,4]) }
{False, True}
>>> { f(x) for f, x in zip(cs, [1,2,2,4]) }
{True}
>>> 

Then map the return value to 0 if it contains a False, else to 1.

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


Thread

Boolean function on variable-length lists Libra <librarama@gmail.com> - 2012-09-12 05:48 -0700
  Re: Boolean function on variable-length lists Jussi Piitulainen <jpiitula@ling.helsinki.fi> - 2012-09-12 16:02 +0300
    Re: Boolean function on variable-length lists Tim Chase <python.list@tim.thechases.com> - 2012-09-12 08:18 -0500
    Re: Boolean function on variable-length lists Libra <librarama@gmail.com> - 2012-09-12 06:19 -0700
      Re: Boolean function on variable-length lists Libra <librarama@gmail.com> - 2012-09-12 06:33 -0700
      Re: Boolean function on variable-length lists Jussi Piitulainen <jpiitula@ling.helsinki.fi> - 2012-09-12 16:37 +0300
        Re: Boolean function on variable-length lists Ken Seehart <ken@seehart.com> - 2012-09-12 06:51 -0700
        Re: Boolean function on variable-length lists Mark Lawrence <breamoreboy@yahoo.co.uk> - 2012-09-12 15:29 +0100
        Re: Boolean function on variable-length lists MRAB <python@mrabarnett.plus.com> - 2012-09-12 16:55 +0100
  Re: Boolean function on variable-length lists Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2012-09-12 13:11 +0000
    Re: Boolean function on variable-length lists Libra <librarama@gmail.com> - 2012-09-12 06:25 -0700

csiph-web