Assume I got two pyomo sets A & B, which are containing following elements:
m.A = {1,2,3,4,5}
m.B = {a,b,c,d,5}
I want to check; if A has some elements which are also in B:
EDIT:
Well following does not work:
if m.A & m.B is not None:
raise ValueError
At least for my case when m.A = [None]
and m.B = ['some_string']
, if-statement is also triggered, but bool(m.A & m.B)
is working.
The most compact way you could achieve this is by using the &
operator:
a = {1,2,3,4}
b = {4,5,6}
result = bool(a & b)
Using the &
operator:
%timeit bool(a & b)
297 ns ± 3.04 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each)
Using the intersection
method:
%timeit bool(a.intersection(b))
365 ns ± 27.1 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each)
The two solution are pretty similar, the second one most probably faces overhead from the method call.