3
$\begingroup$

Suppose, I have a list-

   data = {{1, 2, 3}, {3, 5, 0}, {8, 9, 3}, {2, 5, 0}};

I want to delete second and fourth sublists for which third element of the row is zero. I can do this using

   DeleteCases[data,{_,_,0}];

How can the same operation be achieved efficiently if the list contains a large number of elements instead of just 3?

$\endgroup$
  • $\begingroup$ That depends on where the zero is going to be. E.g. the third element, the last element or some other position. But probably the pattern __ (BlankSequence[]) is what you are after. $\endgroup$ – MikeLimaOscar May 4 '17 at 8:30
  • $\begingroup$ 45429 but use DeleteCases. $\endgroup$ – Kuba May 4 '17 at 8:34
  • 1
    $\begingroup$ Maybe not the most efficient, but incredibly convenient: data /. {_, _, 0} -> Nothing $\endgroup$ – user6014 May 4 '17 at 13:17
10
$\begingroup$

If your data is packed, then the fastest will probably be

Pick[data, Unitize@data[[All, m]], 1]

where m is the position where you don't allow a 0. If it is the last element in a row, then you can use -1 for m.

$\endgroup$
0
$\begingroup$

In:

Clear[unitize, pick, n, data]
RandomSeed[1];
n = -1;
data = RandomChoice[Range[0, 10], {10^8, 3}];

AbsoluteTiming[Pick[data, Unitize@data[[All, n]], 1] // Length]

unitize[x_] := unitize[x] = Unitize[x]
pick[xs_, sel_, patt_] := pick[xs] = Pick[xs, sel, patt]
AbsoluteTiming[pick[data, unitize@data[[All, n]], 1] // Length]

Out:

{7.3081, 90913401}
{5.87919, 90913401}
$\endgroup$
  • 1
    $\begingroup$ -1, this doesn't answer the question. The solution would be fine if instead of AnyTrue you had nthTrue, but nthTrue is not a built-in. $\endgroup$ – LLlAMnYP May 4 '17 at 9:50
  • $\begingroup$ Fair enough. I optimised it base on Marius's method. $\endgroup$ – UnchartedWorks May 4 '17 at 12:49
  • $\begingroup$ un-downvoted, but this is essentially identical to the accepted answer + some very odd memoization technique, which I'm not sure what is achieving here. You know, your original approach would have easily been doable as Select[data, #[[n]] != 0 &]. As an aside, I see you using memoization in many of your recent answers, why do you utilize it so heavily? $\endgroup$ – LLlAMnYP May 4 '17 at 13:57
  • 1
    $\begingroup$ In Functional Programming, we call it Pure Function, It's NOT Pure Function in Wolfram Language. Pure Functions in Wolfram Language is just anonymous functions. When I know that a function has no side effect, such as File System, Network, I implement it as a Pure Function(Functional Programming term). $\endgroup$ – UnchartedWorks May 4 '17 at 14:08
  • $\begingroup$ The pure function always evaluates the same result value given the same argument value(s). The function result value cannot depend on any hidden information or state that may change while program execution proceeds or between different executions of the program, nor can it depend on any external input from I/O devices (usually—see below). Evaluation of the result does not cause any semantically observable side effect or output, such as mutation of mutable objects or output to I/O devices. en.wikipedia.org/wiki/Pure_function $\endgroup$ – UnchartedWorks May 4 '17 at 14:08

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.