In my situation, I have a large list of objects L={A,B,C,D,E,...}
and a function Q[]
applying to them, which will produce the values only from the set {0,2,4,6,8}.
(e.g. Q[]
can be Length[]
and my objects ABCDE are lists of certain sizes)
I want to split the list so that I will obtain 5 lists L0,L2,L4,L6,L8
, each list contains only the index of the corresponding elements in L
(Because I want to keep track of the location of those objects in the original list).
Is there an effective way to do this, other than doing a for loop if statement and calling AppendTo[]
adding the index each time?
(The Mathematica function GatherBy[]
will only give the element of the list itself, but not the index in the original list which I want.)
For example, for the input list
L={{1,2},{3,4,5,6},{7,8,9,10},{11,12}}
and
Q=Length
I want
{L0,L2,L4,L6,L8} = {{},{1,4},{2,3},{},{}}
since sublists of length 2 are at positions 1 and 4, length 4 -- at 2 and 3, and there are no sublists of length 0, 6, 8.
Lookup
with the optional argument to handle missing cases (L4 be an empty list). $\endgroup$GatherBy[Range[Length@L], Q[L[[#]]]&]
? $\endgroup$L={{1,2},{3,4,5,6},{7,8,9,10},{11,12}}
,Q=Length[]
, Then{L0,L2,L4,L6,L8} = {{},{1,4},{2,3},{},{}}
$\endgroup$