3
$\begingroup$

There are two lists as follows:

lista = {i, j, k};
listb = {{1, 2}, {3, 4}, {5, 6}};

If i in {1, 2} (namely, $ 1 \leq i \leq 2 $, where 1 and 2 come from the first item of listb), j in {3, 4}, and k in {5, 6}, it will give True. If only one condition is not met, it will give False. How to make it come true? It's best to achieve the function in one line. Thanks.

$\endgroup$
5
$\begingroup$
ClearAll[f0]
f0 = Apply[And] @* MapThread[Between] @* List;

f0[lista, listb]
1 <= i && i <= 2 && 3 <= j && j <= 4 && 5 <= k && k <= 6
f0[ {1, 3, 5}, listb]
True
f0[ {1, 3, 7}, listb]
False

Also

ClearAll[f1, f2, f3, f4, f5, f6]

f1 = And @@ Between @@@ Transpose[{##}] &;

f2 = And @@ MapThread[Between] @ {##} &;

f3 = And @@ LessEqual @@@ MapThread[Riffle] @ {#2, #} &;

f4 = And @@ MapThread[#2 @ # &]@ {#, Between /@ #2}&;

f5 = And @@ MapThread[Apply[LessEqual] @* Riffle] @ {#2, #} &;

f6 = And @@ MapThread[IntervalMemberQ] @ {Interval /@ #2, #} &;
| improve this answer | |
$\endgroup$
  • $\begingroup$ In fact, the lista is very large, lista = {{i1, j1, k1},{i2, j2, k2},{i3, j3, k3}.......{in, jn, kn}}, I want to take listb as a filter to sieve the unwanted data out of the lista. For simplification, I only take a item from lista. Your solution is so perfect that it surprises me,leaving endless aftertaste.Thanks. $\endgroup$ – likehust Apr 13 at 7:55
1
$\begingroup$
i = 2; j = 3; k = 4;
lista = {i, j, k}
listb = {{1, 2}, {3, 4}, {5, 6}}

MapThread[MemberQ[#1, #2] &, {listb, lista}]

Mathematica graphics

Or if you want <=i<= then

MapThread[If[First@#1 <= #2 <= Last@#1, True, False] &, {listb, lista}]

Mathematica graphics

ClearAll[i, j, k];
i = 2; j = 9; k = 4;
lista = {i, j, k}
listb = {{1, 7}, {3, 11}, {5, 6}}

MapThread[If[First@#1 <= #2 <= Last@#1, True, False] &, {listb, lista}]

Mathematica graphics

| improve this answer | |
$\endgroup$
  • $\begingroup$ Thanks for your attention $\endgroup$ – likehust Apr 14 at 1:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.