6
$\begingroup$

How to build a table that counts how many times each element in mainlist occurs in lst1, lst2, lst3, lst4 and which lst1, lst2, lst3, lst4 contains that element. For example, in mainlist, element N6073 has count = 4 and N6073 appears in lst1, lst2, lst3, and lst4. In mainlist, element N12061 has count = 2 and N12061 appears in lst1, lst3.

mainlist = {N6073, N6019, N12061, N6025, N6065, N6071, N6039, N6077, 
  N53023, N22103, N38077, N12093, N12111, N12035, N22117, N12053, 
  N41005, N4019, N22051, N22105}
lst1 = {N12061, N12111, N4019, N41005, N53023, N6019, N6025, N6065, 
  N6073, N6077}
lst2 = {N12053, N12093, N22117, N6019, N6025, N6039, N6073}
lst3 = {N12061, N12111, N4019, N6065, N6071, N6073}
lst4 = {N22103, N4019, N6019, N6025, N6039, N6065, N6073}
$\endgroup$
  • $\begingroup$ Join[lst1, lst2, lst3, lst4] // Counts $\endgroup$ – cvgmt Sep 28 at 1:57
  • $\begingroup$ very nice and can you then identify which of the lst1, lst2, lst3, lst4 each element in the mainlist falls into? $\endgroup$ – PRG Sep 28 at 2:07
7
$\begingroup$
table = {#, Counts[Join[lst1, lst2, lst3, lst4]] @ # /. _Missing -> 0, 
     Function[x, Select[{"lst1", "lst2", "lst3", "lst4"}, 
        MemberQ[x] @ ToExpression[#] &]] @ #} & /@ mainlist;

headers = {"element", "count", "lists"};

grid = Prepend[table, headers];

grid // Grid

enter image description here

Alternative approaches:

SparseArray:

sa = SparseArray[Outer[Boole @* MemberQ,  {lst1, lst2, lst3, lst4}, mainlist, 1]];

parents = Extract[{"lst1", "lst2", "lst3", "lst4"}, 
   List /@ Transpose[sa]["AdjacencyLists"]];

table2 = Transpose[{mainlist, Total[sa, 1], parents}];
table2 == table
 True

RelationGraph:

rg = RelationGraph[MemberQ[ToExpression@#2, #] &, 
  mainlist, {"lst1", "lst2", "lst3", "lst4"}, VertexLabels -> "Name"]

enter image description here

table3 = {#, VertexDegree[rg, #], AdjacencyList[rg, #]} & /@ mainlist;
table3 == table
 True
| improve this answer | |
$\endgroup$
  • $\begingroup$ super clever ... awesome kglr and many thank you's ... prg $\endgroup$ – PRG Sep 28 at 2:20
4
$\begingroup$
Clear["`*"];
mainlist = {N6073, N6019, N12061, N6025, N6065, N6071, N6039, N6077, 
   N53023, N22103, N38077, N12093, N12111, N12035, N22117, N12053, 
   N41005, N4019, N22051, N22105};
lst1 = {N12061, N12111, N4019, N41005, N53023, N6019, N6025, N6065, 
   N6073, N6077};
lst2 = {N12053, N12093, N22117, N6019, N6025, N6039, N6073};
lst3 = {N12061, N12111, N4019, N6065, N6071, N6073};
lst4 = {N22103, N4019, N6019, N6025, N6039, N6065, N6073};
Subtract[#, 1] & /@ Counts[Join[mainlist, lst1, lst2, lst3, lst4]]

The result is

<|N6073 -> 4, N6019 -> 3, N12061 -> 2, N6025 -> 3, N6065 -> 3, 
 N6071 -> 1, N6039 -> 2, N6077 -> 1, N53023 -> 1, N22103 -> 1, 
 N38077 -> 0, N12093 -> 1, N12111 -> 2, N12035 -> 0, N22117 -> 1, 
 N12053 -> 1, N41005 -> 1, N4019 -> 3, N22051 -> 0, N22105 -> 0|>
| improve this answer | |
$\endgroup$

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.