6

I have a list of lists. For example,

 list = {{0,1,0,1,true},{0,0,0,0,false},{0,1,1,1,true},{1,1,1,1,false},{2,2,2,2,false}}

I'd like to find all lists that have the fifth element set to true. I'm looking for a general method that works for any lists in this format. How can I do this?

In the above example, the result that I'm looking for would be:

 {{0,1,0,1,true},{0,1,1,1,true}}

Sorry for such a basic question. I looked through the documentation for lists and couldn't find a way to do this. I'm still fairly new at Mathematica.

codebpr
  • 2,233
  • 1
  • 7
  • 26
Matt Groff
  • 1,141
  • 1
  • 10
  • 15
  • 3
    Have you tried Cases or Select? true is a proper symbol, but be aware that Mathematica represents truth values with True and False (not true) – Szabolcs Mar 25 '14 at 15:34
  • 1
    Cases[{{0, 1, 0, 1, true}, {0, 0, 0, 0, false}, {0, 1, 1, 1, true}, {1, 1, 1, 1, false}, {2, 2, 2, 2, false}}, {___, true, ___}], see also DeleteCases e.g. in this answer – Artes Mar 25 '14 at 15:41

4 Answers4

9

There are several options and this is probably a duplicate although I can't seem to find it. A few of them to try out and learn:

list = {{0, 1, 0, 1, true}, {0, 0, 0, 0, false}, {0, 1, 1, 1, true}, 
        {1, 1, 1, 1, false}, {2, 2, 2, 2, false}};

Cases[list, {__, true}]
Select[list, Last@# === true &]
DeleteCases[list, {__, false}]
Pick[list, list, {__, true}] /. {} -> Sequence[]

all of which return

(* {{0, 1, 0, 1, true}, {0, 1, 1, 1, true}} *)

If your data has True and False instead of true and false, then you can simplify the Select example to:

Select[list, Last]
rm -rf
  • 88,781
  • 21
  • 293
  • 472
3

as @Szabolcs pointed out, MMA is case sensitive and true is treated as a symbol and not as a variable, thus.

data = {{0, 1, 0, 1, true}, {0, 0, 0, 0, false}, {0, 1, 1, 1, 
true}, {1, 1, 1, 1, false}, {2, 2, 2, 2, false}};
Select[data, MatchQ[#[[5]], true] &]
(*{{0, 1, 0, 1, true}, {0, 1, 1, 1, true}}*)

If the data is entered accordingly (with True and False) then

data = {{0, 1, 0, 1, True}, {0, 0, 0, 0, False}, {0, 1, 1, 1, 
   True}, {1, 1, 1, 1, False}, {2, 2, 2, 2, False}};    
Select[data, TrueQ[#[[5]]] &]
(*{{0, 1, 0, 1, True}, {0, 1, 1, 1, True}}*)
Zviovich
  • 9,308
  • 1
  • 30
  • 52
2
list = {{0, 1, 0, 1, true}, {0, 0, 0, 0, false}, {0, 1, 1, 1, true},
        {1, 1, 1, 1, false}, {2, 2, 2, 2, false}};

Using ReplaceList:

ReplaceList[list, {___, s : {__, Except[false]}, ___} :> s]

({{0, 1, 0, 1, true}, {0, 1, 1, 1, true}})

Or using Replace:

Replace[list, s : {__, false} :> Nothing, {1}]

({{0, 1, 0, 1, true}, {0, 1, 1, 1, true}})

E. Chan-López
  • 23,117
  • 3
  • 21
  • 44
1
list = 
 {{0, 1, 0, 1, true}, {0, 0, 0, 0, false}, {0, 1, 1, 1, true}, 
  {1, 1, 1, 1, false}, {2, 2, 2, 2, false}}

Using SequenceSplit (new in 11.3)

Catenate @ SequenceSplit[list, {{__, false}}]

{{0, 1, 0, 1, true}, {0, 1, 1, 1, true}}

Using Delete

Delete[Position[list, {__, false}]] @ list

{{0, 1, 0, 1, true}, {0, 1, 1, 1, true}}

eldo
  • 67,911
  • 5
  • 60
  • 168