4

I have a list of lists

proof = List[{-1, 2, 3, -1}, {-1, 2, 4, -1}, {-1, 7, 9, -1}, {-1, 7, 2, -1}, 
  {-1, 3, 9, -1}, {-1, 7, 9, -1}, {-1, 5, 9, -1}];

I would like to obtain the same list discarding the first and last entry of each sublist (those marked with -1.

Rest[Most[proof[[1]]]]

works for one of the lists, but when I tried

Rest[Most[proof[[All]]]]

it doesn't work and the output is the same as

Rest[Most[proof]]

(it discards the two external sublists).

I could use

proof = Rest[Reap[Do[Sow[Rest[Most[proof[[k]] ]]], {k,1,7}]]]

Rest is used to discard a NULL result and this returns a triple nested list. Is there an easier/elegant way of doing this?

m_goldberg
  • 107,779
  • 16
  • 103
  • 257
Three Diag
  • 685
  • 4
  • 12

3 Answers3

12
proof[[All, 2;;-2]] (* Shortest and most likely to be the fastest *)
Rest /@ Most /@ proof
Rest @* Most /@ proof (* thanks: Bob Hanlon *)
Composition[Rest, Most] /@ proof
ArrayPad[proof, {{0}, -1}]
Cases[proof, {_, x___, _} :> {x}]
Replace[proof, {_, a___, _} :> {a}, 1] (* thanks: eldo *)
ReplacePart[proof, {{_, 1}, {_,-1}}:> Nothing]
Extract[proof, {All, 2 ;; -2}] (* version 10+ only *)
MapAt[Nothing, proof, {{All, {1,-1}}}] (* version 10+ only *)
Drop[RotateLeft /@ proof, None, -2]

all give

{{2, 3}, {2, 4}, {7, 9}, {7, 2}, {3, 9}, {7, 9}, {5, 9}}

And, for fun, so does

#[[2 ;; -2]] & @ proof 

enter image description here

kglr
  • 394,356
  • 18
  • 477
  • 896
  • The last can be written more compactly as Rest@*Most /@ proof – Bob Hanlon Jul 08 '17 at 14:10
  • Thank you @Bob. Still in version 9 here. I will add your suggestion. – kglr Jul 08 '17 at 14:22
  • For completeness: Replace[proof, {_, a___, _} :> {a}, 2] – eldo Jul 09 '17 at 10:29
  • Thank you @eldo. Added a few more. – kglr Jul 09 '17 at 10:37
  • 2
    While it is fun to find as many ways as possible to do a certain thing, it might also be helpful to the OP to note which method should be considered the optimal for large data. For OP's problem, it doesn't really matter, of course, but I would like to chip in that proof[[All, 2 ;; -2]] should be considered preferable for general arrays of data. – Sjoerd Smit Jul 09 '17 at 15:39
  • @SjoerdSmit, good point. – kglr Jul 09 '17 at 15:42
  • 2
    @eldo Don't you mean Replace[. . ., 1]? – Mr.Wizard Jul 10 '17 at 00:27
  • Oh yes, thanks :) – eldo Jul 10 '17 at 08:13
  • A shorter syntax: ArrayPad[proof, {{0}, -1}]. Speaking of syntax I notice that you replaced All with ;;; despite being a leading proponent of code brevity I prefer All because it costs but a single character for backward-compatibility and explicitness. – Mr.Wizard Jul 10 '17 at 19:09
  • Thank you @Mr.Wizard. Changed both. – kglr Jul 10 '17 at 20:39
2

For your special list (- + + ... + -)

Pick[#, Sign @ #, 1]& @ proof

{{2, 3}, {2, 4}, {7, 9}, {7, 2}, {3, 9}, {7, 9}, {5, 9}}

eldo
  • 67,911
  • 5
  • 60
  • 168
  • 1
    As the OP,I would like to obtain the same list discarding the first and last entry of each sublist. It is not a good solution.But anyway,it is a witty usage of Sign. – yode Jul 09 '17 at 05:37
1
SequenceCases[proof, {{_, x__, _}} :> {x}];
(* {{2, 3}, {2, 4}, {7, 9}, {7, 2}, {3, 9}, {7, 9}, {5, 9}} *)

ReplaceList[proof, {___, {_, a__, _}, ___} :> {a}] (* one of the many ways *)
(* {{2, 3}, {2, 4}, {7, 9}, {7, 2}, {3, 9}, {7, 9}, {5, 9}} *)

proof /. {_, x__Integer, _} :> {x}
(* {{2, 3}, {2, 4}, {7, 9}, {7, 2}, {3, 9}, {7, 9}, {5, 9}} *)

(* since only first and last entries are -1, you can do*)
DeleteCases[proof, -1, 2]
or 
DeleteCases[proof, Except[Integer, -1], 2]
(* {{2, 3}, {2, 4}, {7, 9}, {7, 2}, {3, 9}, {7, 9}, {5, 9}} *)
Ali Hashmi
  • 8,950
  • 4
  • 22
  • 42