I am trying to split a list based on pattern of some elements of the list.
list = {a^(-1-x1),b^(-x2), (a+b)^(-x3), (1-a)^(-1+x4),(1-b)^(-1), (1+a b)}
I want to split the list into two based on a pattern given as a list. I am using SequenceCases
to achieve that. However it does not work for more than two patterns given as a list.
pattern = {Power[a,x_],Power[b,y_],Power[1-a,z_]};
list1=SequenceCases[list,pattern]
whose output (expected) should be
list1={a^(-1-x1),b^(-x2), (1-a)^(-1+x4)}
list2={(a+b)^(-x3),(1-b)^(-1), (1+a b)}
Note that I do not want to split the list based on position of elements, rather based on certain pattern/members of the list.
Additionally I wanted to ask if there is any easier way to find all the elements which have negative exponent (assuming all non-numeric variables in the exponent are 0
.).
{a^(-1-x1), (1-a)^(-1+x4), (1-b)^(-1)}
I can perform a Do
loop through the elements of the list, however it would be nice to know if there is any better alternative.
GroupBy[list, MatchQ[#, Alternatives @@ pattern] &]
That groups into aTrue
group that matches the pattern and aFalse
group that does not. You can access viaresult[True]
orLookup[result, True]
$\endgroup$