3
$\begingroup$

I have a list of 30,000 elements, each of the element is a list in itself {{a, b}, {p, q, r}, {a, p}, ..., {z, x}}. Second one contains unique elements {a, b, c, ..., z}.

I want to create an association which associates all the unique elements to the first list so that it shows whether the corresponding element is present in the first list. So, it should look something like this:

{
  {<|a -> True, b -> True, c -> False, ..., z -> False|>},
  {<| a -> False, b -> False, ..., p -> False, q -> False, r -> False, ..., z -> False |>},
   ...
}

Is there a way I can do this without loops?

$\endgroup$
  • $\begingroup$ Welcome to Mathematica.SE! I suggest that: 1) You take the introductory Tour now! 2) When you see good questions and answers, vote them up by clicking the gray triangles, because the credibility of the system is based on the reputation gained by users sharing their knowledge. Also, please remember to accept the answer, if any, that solves your problem, by clicking the checkmark sign! 3) As you receive help, try to give it too, by answering questions in your area of expertise. $\endgroup$ – bbgodfrey Jan 30 '15 at 19:52
  • $\begingroup$ What I can't understand is why you want to wrap each association in List. Why not just a list of associations? $\endgroup$ – m_goldberg Jan 30 '15 at 22:12
  • $\begingroup$ @m_goldberg I didn't quite get you, you mean '{<|a -> True, False ... |>, <| b -> True, True ... |>, ... }' ? $\endgroup$ – babbupandey Jan 31 '15 at 4:47
  • $\begingroup$ I mean {{<|a -> True, b -> True, c -> False, ..., z -> False|>}, {<| a -> False, b -> False, ..., p -> False, q -> False, r -> False, ..., z -> False |>}, ...}. $\endgroup$ – m_goldberg Jan 31 '15 at 12:15
  • $\begingroup$ yes, this is exactly I want and the answer of @kuba gives this output :) $\endgroup$ – babbupandey Feb 1 '15 at 4:35
4
$\begingroup$
 ClearAll @@ CharacterRange["a", "z"]

 elements = Symbol /@ CharacterRange["a", "z"]

 main = Association @ Thread[elements -> False]
<|a->False,b->False,c->False ... x->False,y->False,z->False|>
set = {{a, b}, {p, q, r}, {a, p}}

Merge[{Association@Thread[# -> True], main}, Or @@ # & ] & /@ set
{<|a -> True, b -> True, c -> False, ... x -> False, y -> False, z -> False|>, 
  <|p -> True, q -> True, r -> True, a -> False, ... z -> False|>, 
  <|a -> True, p -> True, b -> False, ... y -> False, z -> False|>}
$\endgroup$
  • $\begingroup$ I'm not very familiar with Associations so it may not be the best approach. $\endgroup$ – Kuba Jan 30 '15 at 19:57
  • $\begingroup$ Just for my understanding, you first created a association with all False values, then you merged this association with another association containing True values for the corresponding elements from the list. Am I correct? $\endgroup$ – babbupandey Jan 30 '15 at 21:17
  • $\begingroup$ @babbupandey Yes, first main and then every sublist is (while mapping) converted in similar way to ->True and marged with main, duplicates are handled with Or function which will convert a -> {True, False} to a -> Or@@{True,False} == True $\endgroup$ – Kuba Jan 30 '15 at 21:21
  • $\begingroup$ Ah, ok! I accepted it because this worked pretty fast compared to the loops I was running :-) $\endgroup$ – babbupandey Jan 30 '15 at 21:36
  • $\begingroup$ @babbupandey I'm glad it did. Loops are usually not so fast in Mathematica. You may be interested in Alternatives to procedural loops. $\endgroup$ – Kuba Jan 30 '15 at 21:38

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.