I have tripletList
, a list of triplets of integers:
tripletList = {{1, 2, 5}, {1, 2, 6}, {2, 1, 4}, {2, 2, 3}, {2, 2, 4}};
I wish to delete triplets whose second part is 1. In other words, I wish to operate on tripletList
to obtain this result:
{{1, 2, 5}, {1, 2, 6}, {2, 2, 3}, {2, 2, 4}}
I realized this is a somewhat nebulous, subjective question, but... what is the "best" or "Wolfram canonical" way to accomplish this? I can think of three approaches:
(* (1) *)
DeleteCases[tripletList, _?(#[[2]] == 1 &)] // AbsoluteTiming
(* (2) *)
Replace[tripletList,
element_ /; (element[[2]] == 1) -> Nothing, {1}] // AbsoluteTiming
(* (3) *)
Delete[#, Position[#, {_, 1, _}]] &@tripletList // AbsoluteTiming
(* (4) *)
DeleteCases[tripletList, {_, 1, _}] // AbsoluteTiming
(* OUTPUT from (1), (2), (3), and (4), respectively: *)
(* {0.0000147, {{1, 2, 5}, {1, 2, 6}, {2, 2, 3}, {2, 2, 4}}} *)
(* {0.0000125, {{1, 2, 5}, {1, 2, 6}, {2, 2, 3}, {2, 2, 4}}} *)
(* {9.2*10^(-6), {{1, 2, 5}, {1, 2, 6}, {2, 2, 3}, {2, 2, 4}}} *)
(* {3.8*10^(-6), {{1, 2, 5}, {1, 2, 6}, {2, 2, 3}, {2, 2, 4}}} *)
Clearly, approach (4) is the fastest, being about four times faster than approach (1). Is it fair to say that Wolfram is more or less built on the pattern-matching paradigm?