zhenyuzeng
(Zhenyu Zeng)
October 13, 2022, 12:55pm
1
Hello,
I am trying to use Select to select something meeting my requirements. For example:
Select[{{1, 2, 3}, {4, 1, 6}}, Abs[#3 - #2] > 3 &]
I hope this can select lists in which Abs[#3-#2]>3, such as {4,1,6}.
But I see the code above does not work rightly. Where did I wrong and what is the right code?
ahmed
(Ahmed Elbanna)
October 13, 2022, 1:54pm
2
You need to use Part in this case
Select[{{1, 2, 3}, {4, 1, 6}}, Abs[#[[3]] - #[[2]]] > 3 &]
You could also use Cases
Cases[{{1, 2, 3}, {4, 1, 6}}, {_, x_, y_} /; Abs[y - x] > 3]
zhenyuzeng
(Zhenyu Zeng)
October 13, 2022, 2:09pm
4
Yes, I just can’t image this way myself. When you pointed it out, I can understand. Thanks.
zhenyuzeng
(Zhenyu Zeng)
October 13, 2022, 2:10pm
5
Good way. Cases is more vivid. Thanks.