Boolean function on variable-length lists

Ken Seehart ken at seehart.com
Wed Sep 12 09:51:46 EDT 2012


Putting a few of peoples ideas together...


gt = lambda x: lambda y: x>y
eq = lambda x: lambda y: x==y

def constrain(c,d):
    return all({f(x) for f, x in zip(c, d)})

constraints = [gt(2), eq(1)]
data0 = [1,1]
data1 = [3,1]
   
print constrain(constraints, data0)
print constrain(constraints, data1)




On 9/12/2012 6:37 AM, Jussi Piitulainen wrote:
> Libra writes:
>> On Wednesday, September 12, 2012 3:02:44 PM UTC+2, Jussi Piitulainen wrote:
>>  
>>> So you would associate each constraint with an index. You could
>>> maintain a list of constraints and apply it to the values as
>>> follows:
>> Yes, even though there could be more constraints for each value in
>> the list (at least 1 constraint for each value)
> Either you write more complex constraint functions, or you use more
> complex data structures to hold them.
>
>>>>>> 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]) }
>> Just to understand, with f(x) you are defining a function f with
>> argument x, right? I didn't know it was possible to define functions
>> in this way. Is this a case of anonymous function?
> The value of each lambda expression is a function. f(x) is a function
> call, evaluated for each pair (f, x) from the list of pairs that the
> zip returns.
>
> { ... for ... in ... } creates a set of the values, no duplicates.
> [ ... for ... in ... ] creates a list of the values.
>
>>> {False, True}
>> Actually, I don't understand the output. Why it is both False and
>> True?
> It's a set containing False and True. The False comes from the f(x)
> where f = lambda x : x == 2, and x is 3. There is only one True
> because I requested a set of the values.


-------------- next part --------------
A non-text attachment was scrubbed...
Name: smime.p7s
Type: application/pkcs7-signature
Size: 3736 bytes
Desc: S/MIME Cryptographic Signature
URL: <http://mail.python.org/pipermail/python-list/attachments/20120912/8efd15e5/attachment.bin>


More information about the Python-list mailing list