# Collect the elements from a list by categories [closed]

I have a list of elemets with different variables {a,b,c,A,B,C},

list={3 b, A Cos[c]^2 Sin[a] Sin[b], -a E^(I a c) f[C, A]}


Where f is some function. By assuming a, b, c, and integers are one category and A, B, C are the other category, I want everything associates with (a b c, and integers) to be in the first list, and the other to be in the second list. so I want to divide this list by the two groups such that {group1,group2}.

(*{group1,group2}*)
(*1 means there is no element in the list for the category*)

{{3 b, Cos[c]^2 Sin[a] Sin[b], -a E^(I a c)},
{1, A, f[C, A]}}


I was trying to use Select, but I could not be able to do it because arbitrary function such as Cos, Sin, and Exp makes it difficult to separate. Any suggestion? Thank you

I modified list to make it easier to understand

• So you want factors of each term containing A, B, C in list 2, and the other factors in list 1? – lirtosiast Jun 29 '19 at 8:30

This function takes an expression and sorts its factors between a list of two elements depending on whether they contain one of A, B, C. From there it is easy to build what you want:

list={3 b, A Cos[c]^2 Sin[a] Sin[b], -a E^(I a c) f[C, A], B, 1, Pi};

factorByABC[expr_] := With[{x = Switch[expr, _Times, List@@expr, _, List@expr], hasABC = !FreeQ[#, A | B | C]&},
Times @@@ {Select[x, Not@*hasABC], Select[x, hasABC]}
];

factorByABC /@ list // Transpose // Print

(* {{3*b, Cos[c]^2*Sin[a]*Sin[b], -(a*E^(I*a*c)), 1, 1, Pi}, {1, A, f[C, A], B, 1, 1}} *)


Try it online!

• Thank you so muich! – Saesun Kim Jun 29 '19 at 16:27
selectDiscard = Map[Select @ # @* FreeQ[a|b|c|_Integer]]& /@ {Not, Identity}

list = {3 b, A Cos[c]^2 Sin[a] Sin[b], -a E^(I a c) f[C, A]};


{{3 b, Cos[c]^2 Sin[a] Sin[b], -a E^(I a c)},
{1, A, f[C, A]}}

You can use FreeQ and Complement:

list = {a*A*b*Cosh[c]^2, 3*b*c*Sinh[a],
Sinh[a]*Sinh[b]*A, -a*c*CenterDot[C, A]*E^(I*a*c)};

Union[Flatten[Outer[If[! FreeQ[#1, #2], #1, Nothing] &, list, {a, b, c}]]]

(* {-a c E^(I a c) C\[CenterDot]A, a A b Cosh[c]^2, 3 b c Sinh[a], A Sinh[a] Sinh[b]} *)

Union[Flatten[Outer[If[! FreeQ[#1, #2], #1, Nothing] &, list, {A, B, C}]]]

(* {-a c E^(I a c) C\[CenterDot]A, a A b Cosh[c]^2, A Sinh[a] Sinh[b]} *)

Complement[list, %, %%]

(* {} *)

• Thank you for your reply, maybe my post was confusing, so I modified it. Please check :D – Saesun Kim Jun 28 '19 at 23:46