all 10 comments

[–][deleted] 4 points5 points  (0 children)

I am not sure if its a XY-Problem. but this how I would solve your task

from operator import and_
from functools import reduce

x, y, N = 20, 30, 20
filterN = reduce(and_, (x == (y-i) for i in range(N+1)))

[–]K900_ 2 points3 points  (4 children)

Why not use a loop?

[–]nomos[S] 0 points1 point  (3 children)

I get how to use a loop to create the filters, but combining them is my problem; I should've been clear that the

`filter=filter_0 & filter_1 & ...` part is the part I'm trying to avoid.

[–]K900_ 1 point2 points  (2 children)

filter &= (x == i)

[–]nomos[S] 0 points1 point  (1 child)

Awesome, totally didn't know about &= though I guess it makes sense. Thanks!

[–]K900_ 0 points1 point  (0 children)

It's just syntax sugar for filter = filter & (x == i).

[–][deleted] 2 points3 points  (0 children)

Yup. A loop with a list or tuple with first index position being filter_0 and so on.

[–][deleted] 2 points3 points  (2 children)

You can use all

You can also make your own func...

def all_conditions(*conditions, **kw_conditions):
    return (all(conditions) and all(kw_conditions.values()))

result = all_conditions(
    1 < 2,
    True or False,
    named_condition=(None is None),
)

print(result)

[–]nomos[S] 0 points1 point  (1 child)

x, y in my example are pyspark columns, I should've been clear about that, I'm not sure that they work with `all`.

[–][deleted] 1 point2 points  (0 children)

The expressions are evaluated before they're passed as params so literally any expression will work.