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


Groups > comp.lang.python > #28958

Re: Boolean function on variable-length lists

Date 2012-09-12 08:18 -0500
From Tim Chase <python.list@tim.thechases.com>
Subject Re: Boolean function on variable-length lists
References <f9d9dfa2-71c6-4b47-9d0a-e6f59c8ab818@googlegroups.com> <qotwqzzjswb.fsf@ruuvi.it.helsinki.fi>
Newsgroups comp.lang.python
Message-ID <mailman.556.1347455871.27098.python-list@python.org> (permalink)

Show all headers | View raw


On 09/12/12 08:02, Jussi Piitulainen wrote:
> Libra writes:
>> 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
> 
> 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 ]

This can even be decoupled a bit more for dynamic creation:

>>> lst = [1,2,3,4]
>>> import operator as o
>>> conditions = [
...     (o.ge, 1),
...     (o.le, 3),
...     (o.eq, 2),
...     (o.ge, 3),
...     ]
>>> [op(v, constraint) for ((op, constraint), v) in zip(conditions,
lst)]
[True, True, False, True]
>>> all(compare(value, constraint) for ((compare, constraint),
value) in zip(conditions, lst))
False

Note that you'd also want to check len(conditions)==len(lst) for
obvious reasons. :-)

-tkc



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