dict to boolean expression, how to?

Alex van der Spek zdoor at xs4all.nl
Fri Aug 1 08:45:12 EDT 2014


With a dict like so:

cond = {'a': 1, 'b': 1, 'c': 1,
        'A': 0, 'B', 0, 'C':0}

how would you make a boolean expression like this:

bool = (('a' == 1) & ('A' == 0) |
        ('b' == 1) & ('B' == 0) |
        ('c' == 1) & ('C' == 0))

The fact that lowercase and uppercase keys are stringed together with & is 
intentional albeit the actual condition is a bit more tricky.

I've tried several approaches using eval() on a string built from the dict 
but landed with just spelling it out literally.


Any pointers welcome.
Alex



More information about the Python-list mailing list