I have list of associations

lr1 = {<|a -> {0,0}, b -> "TRUE"|>, <|b -> "FALSE", a -> {0,1}|>, <|a -> {1,0}, b -> "FALSE"|>, <|b -> "TRUE", a -> {1,1}|>}

and I would like to count number of associations in a list lr1 that matches pattern that key b is associated with value "TRUE".

Similarly, to extract new list of associations, with only those that matches the sam pattern as in case of counting.

Keys a and b does not necessary have unique positions through associations.

I need to solve this with Mathematica functions Count and Cases.

  • $\begingroup$ "I need to solve this with Mathematica functions Count and Cases." - why the restriction, if other approaches can be more compact or efficient? $\endgroup$ – J. M. is away Nov 12 '17 at 10:07
  • $\begingroup$ @J.M. - the list of associations is few x 10^6, so primary function is to count acceptable assoc. Next step is extraction. I would avoid making any operations before counting. So, this is reason for counting first. But if this is possible, without generating new datalist please post it. $\endgroup$ – Dragutin Nov 12 '17 at 10:19
  • 4
    $\begingroup$ So, would Length @* Select[MatchQ["TRUE"] @* Lookup[b]] @ lr1 fit the bill? $\endgroup$ – J. M. is away Nov 12 '17 at 10:20
  • 1
    $\begingroup$ That's the infix form of Composition[]. $\endgroup$ – J. M. is away Nov 12 '17 at 11:17
  • 4
    $\begingroup$ Count[Lookup[lr1, b], "TRUE"] or Count[lr1, "TRUE", {0, ∞}] $\endgroup$ – Coolwater Nov 12 '17 at 14:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.