Counting Elements in Multiple Lists
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
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"]
table3 = {#, VertexDegree[rg, #], AdjacencyList[rg, #]} & /@ mainlist;
table3 == table
True
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|>