I have a list like:
list = {{3, 1, 4}, {1, 5}, {9, 2, 6}, {5, 3}, {5, 8, 9, 7}, {9, 3},
{2}, {3, 8, 4, 6, 2, 6}, {4, 3, 3}, {8}, {3, 2, 7, 9, 5, 0}}
Now I can select all nested lists of length 6:
Select[list, Length[#] == 6 &]
which gives the following result:
{{3, 8, 4, 6, 2, 6}, {3, 2, 7, 9, 5, 0}}
Now I want to create lists like this for lengths 1, 2, ... maximum length. I understand I'll need
Sort[DeleteDuplicates[Length /@ list]]
which gives
{1, 2, 3, 4, 6}
How do I run over this list with the Select
function above?
GatherBy[list,Length]
do what you want? $\endgroup$Table[Set[x[Last@Dimensions@i], i], {i, GatherBy[list, Length]}]
will gather the lists then set them to the values ofx[len]
, wherex[1]
is the length 1 lists, andx[2]
all the length 2 lists etc. $\endgroup$